PHP-based cURL Quick Start 3 the following code is a bit complicated, so I will explain it in a small step: the reference content is as follows: // 1. batch processor $ mh = curl_multi_init (); // 2. add URLfor ($ I = 0; $ I & lt; $ max _ PHP-based cURL Quick Start 3
The following code is a bit complicated, so I will explain it in a small step:
Reference content is as follows:
// 1. batch processor $ mh = curl_multi_init (); // 2. add the URLfor ($ I = 0; $ I <$ max_connections; $ I ++) {add_url_to_multi_handle ($ mh, $ url_list);} // 3. do {$ mrc = curl_multi_exec ($ mh, $ active);} while ($ mrc = CURLM_CALL_MULTI_PERFORM); // 4. main loop while ($ active & $ mrc = CURLM_ OK) {// 5. active connection if (curl_multi_select ($ mh )! =-1) {// 6. do {$ mrc = curl_multi_exec ($ mh, $ active);} while ($ mrc = CURLM_CALL_MULTI_PERFORM); // 7. is there any information? If ($ mhinfo = curl_multi_info_read ($ mh) {// indicates that the connection ends normally. // 8. obtain information from the curl handle $ chinfo = curl_getinfo ($ mhinfo ['handle']); // 9. dead chain? If (! $ Chinfo ['http _ Code']) {$ dead_urls [] = $ chinfo ['URL']; // 10. 404 ?} Else if ($ chinfo ['http _ Code'] = 404) {$ not_found_urls [] = $ chinfo ['URL']; // 11. you can also use} else {$ working_urls [] = $ chinfo ['URL'];} // 12. remove the handle curl_multi_remove_handle ($ mh, $ mhinfo ['handle']); curl_close ($ mhinfo ['handle']); // 13. add a new URL to work. if (add_url_to_multi_handle ($ mh, $ url_list) {do {$ mrc = curl_multi_exec ($ mh, $ active );} while ($ mrc = CURLM_CALL_MULTI_PERFORM) ;}}}// 14. curl_multi_close ($ mh); echo "= Dead URLs = \ n"; echo implode ("\ n", $ dead_urls ). "\ n"; echo "= 404 URLs = \ n"; echo implode ("\ n", $ not_found_urls ). "\ n"; echo "= Working URLs = \ n"; echo implode ("\ n", $ working_urls); // 15. add urlfunction add_url_to_multi_handle ($ mh, $ url_list) {static $ index = 0; // if there is no url left, if ($ url_list [$ index]) {// Create a curl handle $ ch = curl_init (); // Configure urlcurl_setopt ($ ch, CURLOPT_URL, $ url_list [$ index]); // do not want to output the returned content curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1) where the redirection is located ); // no content body is required, which can save bandwidth and time. curl_setopt ($ ch, CURLOPT_NOBODY, 1); // add it to the batch processor. curl_multi_add_handle ($ mh, $ ch ); // Dial the counter. The next time you call this function, you can add the next url $ index ++; return true ;} else {// no new URL needs to handle return false ;}}
?
The above code is explained below. The serial number of the list corresponds to the sequential number in the code comment.
Create a new batch processor. Created a multi handle.
Later, we will create a function add_url_to_multi_handle () that adds the URL to the batch processor (). Each time this function is called, a new url is added to a batch processor. At the beginning, we added 10 URLs to the batch processor (this number is determined by $ max_connections ).
It is required to run curl_multi_exec () for initialization, as long as it returns CURLM_CALL_MULTI_PERFORM, there is still something to do. To create a connection, it does not wait for the complete URL response.
As long as there are active connections in the batch processing, the main cycle will continue.
Curl_multi_select () will wait until a URL query generates an active connection.
CURL is another task, mainly to obtain response data.
Check various information. When a URL request is complete, an array is returned.
The returned array contains a cURL handle. We use it to obtain the corresponding information of a single cURL request.
If this is a dead link or the request times out, no http status code is returned.
If this page cannot be found, status code 404 is returned.
In other cases, we all think that this link is available (of course, you can also check the 500 error and so on ...).
Remove the cURL handle from this batch, because it has no utilization value, and it is disabled!
Well, now you can add another URL. Again, the initialization starts again...
Well, it's all done. Disable the batch processor to generate reports.
Let's look at the function of adding a new URL to the batch processor. Every time this function is called, the static variable $ index increments once so that we can know how many URLs are not processed.
I ran this script on my blog (test required, some error links were intentionally added). The result is as follows:
Reference content is as follows:
?
A total of about 40 URLs are checked, which takes less than two seconds. When you need to check a large number of URLs, the effect of worry-free and effort-saving can be imagined! If you open 10 connections at the same time, it will be 10 times faster! In addition, you can use the no-partition feature of cURL batch processing to process a large number of URL requests without blocking your Web scripts.
?
Other useful cURL options
HTTP authentication
If a URL request requires HTTP-based authentication, you can use the following code:
Copy the content to the clipboard code:
Reference content is as follows:
$ Url = "http://www.somesite.com/members/"; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // send the username and password curl_setopt ($ ch, CURLOPT_USERPWD, "myusername: mypassword"); // you can allow it to redirect curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 ); // the following options enable cURL to send the user name and password curl_setopt ($ ch, CURLOPT_UNRESTRICTED_AUTH, 1) after redirection //; $ output = curl_exec ($ ch ); curl_close ($ ch );
?
FTP Upload
PHP comes with an FTP class library, but you can also use cURL:
Reference content is as follows:
// Open a file pointer $ file = fopen ("/path/to/file", "r"); // url contains most of the required information $ url = "ftp: // username: password@mydomain.com: 21/path/to/new/file "; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, upload, 1); // Upload related options curl_setopt ($ ch, CURLOPT_UPLOAD, 1); curl_setopt ($ ch, CURLOPT_INFILE, $ fp); curl_setopt ($ ch, CURLOPT_INFILESIZE, filesize ("/path/to/file"); // whether to enable the ASCII mode (useful when uploading text files) curl_setopt ($ ch, CURLOPT_FTPASCII, 1 ); $ output = curl_exec ($ ch); curl_close ($ ch );
?
Fan Wall
You can use a proxy to initiate a cURL request:
Reference content is as follows:
$ Ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, 'http: // www.example.com '); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // specify the proxy address curl_setopt ($ ch, CURLOPT_PROXY, '11. 11.11.11: 8080 '); // if necessary, provide the user name and password curl_setopt ($ ch, CURLOPT_PROXYUSERPWD, 'User: pass'); $ output = curl_exec ($ ch ); curl_close ($ ch );
?
Callback function
CURL can call a specified callback function during a URL request. For example, you can use data immediately during content or response download, instead of waiting until the download is complete.
Reference content is as follows:
$ch = curl_init();curl_setopt($ch, CURLOPT_URL,'http://net.tutsplus.com');curl_setopt($ch, CURLOPT_WRITEFUNCTION,"progress_function");curl_exec($ch);curl_close ($ch);function progress_function($ch,$str) {echo $str;return strlen($str);}
?
This callback function must return the length of the string, otherwise this function will not work properly.
In the process of receiving a URL response, this function will be called as long as a packet is received.
Summary
Today, we have learned the powerful functions and flexible scalability of the cURL Library. Hope you like it. Consider cURL for the next URL request!
Original article: PHP-based cURL quick start
Http://net.tutsplus.com/tutorial%20...%20for-mastering-curl/.
Original Author: Burak Guzel
Link: http://www.blueidea.com/tech/program/2010/7348.asp