Home >Backend Development >PHP Tutorial >Analyze the application of curl_multi in php_PHP tutorial
I believe that many people have a headache with the curl_multi family of functions that are unclear in the PHP manual. They have few documents and the examples given are so simple that you cannot learn from them. I have also searched many web pages, but I have not found a complete application. example.
•curl_multi_add_handle
•curl_multi_close
•curl_multi_exec
•curl_multi_getcontent
•curl_multi_info_read
•curl_multi_init
•curl_multi_remove_handle
•curl_multi_select
Generally speaking, when you think of using When using these functions, the purpose should obviously be to request multiple URLs at the same time, rather than requesting them one by one. Otherwise, it is better to loop and adjust curl_exec by yourself.
The steps are summarized as follows:
Step 1: Call curl_multi_init
Step 2: Call curl_multi_add_handle in a loop
What needs to be noted in this step is that curl_multi_add_handle The second parameter is the subhandle from curl_init.
Step 3: Continue to call curl_multi_exec
Step 4: Call curl_multi_getcontent in a loop to obtain the results as needed
Step 5: Call curl_multi_remove_handle, and call curl_close for each word handle
Step 6: Call curl_multi_close
Here is a simple example found online, which the author calls a dirty example (I will explain why dirty later):
$connomains = array(
"http://www.cnn.com/",
"http://www.canada.com/",
"http://www. yahoo.com/"
);
$mh = curl_multi_init();
foreach ($connomains as $i => $url) {
$conn[$i]=curl_init($url);
curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle ($mh,$conn[$i]);
}
do { $n=curl_multi_exec($mh,$active); } while ($active);
foreach ($connomains as $i => $url) {
$res[$i]=curl_multi_getcontent($conn[$i]);
curl_close($conn[$i]);
}
print_r($res);
In addition: There are some details that may sometimes be encountered:
Control the timeout of each request, do it through curl_setopt before curl_multi_add_handle:
curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
To determine whether there is a timeout or other errors, use: curl_error($conn[$i]);
before curl_multi_getcontentNote: Use the multi_curl function of php with caution, because there are bugs in the combination of curl and php in some versions. So the code that you debugged without problems is likely to be incorrect on other machines.
For example, today I discovered that if the CURLOPT_USERAGENT attribute is set to certain values in php5.2.2 with curl/7.16.2, the actual HTTP header sent will become a string of binary values.
It just so happened that the strip_tags function in this version of php did not handle binary data well, so I discovered this bug