5 typical php curl examples and phpcurl classic examples
I use php and curl mainly captures data. Of course we can use other methods to capture data, such as fsockopen and file_get_contents. However, you can only capture those pages that can be accessed directly. It is more difficult to capture pages with page access control, or to log on to pages after logon.
1. capture files without Access Control
<? Php $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, "http: // localhost/mytest/phpinfo. php "); curl_setopt ($ ch, CURLOPT_HEADER, false); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // If you comment out this line, $ result = curl_exec ($ ch); curl_close ($ ch);?>
2. Use a proxy to capture
Why is it necessary to use a proxy for crawling? Take google for example. If google's data is captured frequently in a short period of time, you will not be able to capture it. When google restricts your IP address, you can use another proxy to re-capture it.
<Pre name = "code" class = "php"> <? Php $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, "http://blog.51yip.com"); curl_setopt ($ ch, CURLOPT_HEADER, false); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, CURLOPT_HTTPPROXYTUNNEL, TRUE); curl_setopt ($ ch, CURLOPT_PROXY, 125.21.23.6: 8080); // url_setopt ($ ch, quit, 'user: password '); add $ result = curl_exec ($ ch); curl_close ($ ch);?>
3. capture data after post
Let's talk about data submission separately, because when using curl, there are often data interactions, so it is important.
<? Php $ ch = curl_init ();/* Note that, the data to be submitted cannot be a two-dimensional array or higher * For example, array ('name' => serialize (array ('tank', 'zhang '), 'sex' => 1, 'birth' => '000000') * For example, array ('name' => array ('tank', 'zhang'), 'sex' => 1, */$ data = array ('name' => 'test', 'sex' => 1, 'birth' => '20140901'); curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // localhost/mytest/curl/upload. php '); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CU RLOPT_POSTFIELDS, $ data); curl_exec ($ ch);?>
In upload. in the PHP file, print_r ($ _ POST); Use curl to capture the upload. php output content Array ([name] => test [sex] => 1 [birth] => 20101010)
4. Capture pages with page Access Control
I have written an article before. If you are interested in the three methods of page access control, please take a look.
If you use the method mentioned above, the following error will be reported:
You are not authorized to view this page
You do not have permission to view this directory or page using the credentials that you supplied because your Web browser is sending a WWW-Authenticate header field that the Web server is not configured to accept.
At this time, we will use CURLOPT_USERPWD for verification.
<? Php $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, "http: // club-china "); /* CURLOPT_USERPWD is mainly used to crack page access control * For example, we usually generate Page Control for htpasswd. * // Curl_setopt ($ ch, CURLOPT_USERPWD, 'user: password'); curl_setopt ($ ch, CURLOPT_HTTPGET, 1); curl_setopt ($ ch, CURLOPT_REFERER, "http: // club-china "); curl_setopt ($ ch, CURLOPT_HEADER, 0); $ result = curl_exec ($ ch); curl_close ($ ch);?>
The above five typical examples of php curl are all the content shared by the editor. I hope you can give us a reference and support the help house.