In php, curl cyclically requests multiple URLs and multiple threads to request multiple URLs. The first method is loop request $ srarray (url_1, url_2, url_3 ); foreach ($ sras $ k & gt; $ v) {$ curlPost $ v .? F input parameters; $ chcurl_init ($ curlPost); curl_setopt ($ ch, CURLOP php curl loop to request multiple URLs and multiple threads to request multiple URLs
Type 1: cyclic requests
$ Sr = array (url_1, url_2, url_3); foreach ($ sr as $ k => $ v) {$ curlPost = $ v .'? F = input parameter '; $ ch = curl_init ($ curlPost); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // get data and return curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true ); // when CURLOPT_RETURNTRANSFER is enabled, $ data = curl_exec ($ ch); echo $ k is returned. '##:'. $ data.'
';
}
curl_close($ch);
Second multi-thread request
$ Sr = array (url_1, url_2, url_3 );
$mh = curl_multi_init(); foreach ($sr as $i => $url) { $curlPost=$url.'?f=%2Fpipefile%2Fskincss%2Fskincss_1356490012_1.zip%40%2Fskincss%2Fimages%2Fskincss_1356490012_1.jpg'; $conn[$i]=curl_init($curlPost); curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1); curl_multi_add_handle ($mh,$conn[$i]); } do { $mrc = curl_multi_exec($mh,$active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active and $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } foreach ($sr as $i => $url) { $res[$i]=curl_multi_getcontent($conn[$i]); curl_close($conn[$i]); } var_dump($res);
After a simple test, it is found that when four different URLs are requested at the same time, the processing speed of the loop seems to be faster than that of multithreading. This problem needs to be verified.