Curl Batch processing
Curl also has an advanced feature- batch handle (handle). This feature allows multiple curl connections to be opened simultaneously or asynchronously.
"Sample":
<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" ><?
PHP//Create two curl resources $ch 1 = curl_init ();
$ch 2 = Curl_init ();
Specifies the URL and the appropriate parameter curl_setopt ($ch 1,curlopt_url, "http://lxr.php.net/");
curl_setopt ($ch 1,curlopt_header,0);
curl_setopt ($ch 2,curlopt_url, "http://www.php.net/");
curl_setopt ($ch 2,curlopt_header,0);
Create Curl Batch Handle $MH = Curl_multi_init ();
Add a handle to the previous two resources curl_multi_add_handle ($MH, $ch 1);
Curl_multi_add_handle ($MH, $ch 2);
Predefined one state variable $active = NULL;
Execute batch processing do{$MRC = Curl_multi_exec ($MH, $active);
}while ($MRC = = Curlm_call_multi_perform);
while ($active && $MRC ==curlm_ok) {if (Curl_multi_select ($MH)!=-1) {do{
$MRC =curl_multi_exec ($MH, $active);
}while ($MRC ==curlm_call_multi_perform);
}///close each handle Curl_multi_remove_handle ($MH, $ch 1); Curl_multi_remove_handle ($MH, $ch 2); Curl_multi_close ($MH);?></span>
"Resolution":
The thing to do here is to open multiple curl handles and assign them to a batch handle, and then just wait in a while loop to complete the execution.
The first do ... while loop repeats the invocation of Curl_multi_exec (). This function is not partition-free. But will do as little as possible. It returns a state value, as long as the value equals constant curm_call_multi_perform, there is still work to be done. (for example, send the HTTP header information of the corresponding URL), that is, the function needs to be called continuously until the return value changes.
The next while loop, which is passed to Curl_multi_exec () as the second argument before continuing the variable when the $active variable is true, represents whether there are active connections in the batch handle as well. Then call Curl_multi_select (), which is blocked until the active connection appears. Once this function succeeds, it goes into another do...while loop and continues to the next URL.
"description":
Many people call this approach curl multithreading, and curl_multi_exec is not a multithreaded, it belongs to the category of asynchronous processing.