I believe many people have a headache about the curl_multi family of functions that are unclear in the php manual. They have few documents and the examples given are even simpler. So that you have nothing to learn from, I have searched many web pages, but I have not seen a complete application example.
Generally speaking, when you think of 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 adjust curl_exec in a loop 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 the second parameter of curl_multi_add_handle is the subhandle from curl_init.
Step 3: Continue to call curl_multi_exec
Step 4: Call curl_multi_getcontent in a loop as needed to obtain the results
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.baidu.com/",
"http://www.google.com/",
"http://www.jb51.net/"
);
$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);
The entire usage process is almost like this. However, this simple code has a fatal weakness, that is, in the do loop, it is an infinite loop during the entire url request, which can easily cause the CPU to occupy 100%.
Now let’s improve it. Here we need to use a function curl_multi_select that has almost no documentation. Although C’s curl library has instructions for select, the interface and usage in PHP are indeed different from those in C.
Change the do paragraph above to the following:
Because $active has to wait until all url data is received before it becomes false, so the return value of curl_multi_exec is used here to determine whether there is still data. When there is data, curl_multi_exec will be called continuously. If there is no data temporarily, it will enter the select stage. , it can be awakened to continue execution as soon as new data comes. The advantage here is that there is no unnecessary consumption of CPU.
Also: There are some details that you may encounter sometimes:
To control the timeout of each request, do it through curl_setopt before curl_multi_add_handle:
To determine whether there is a timeout or other errors, use: curl_error($conn[$i]);
before curl_multi_getcontent