PHP in a loop to request multiple URLs and multithreading to request multiple URLs method
The first type: Circular request
$SR =array (Url_1,url_2,url_3), foreach ($sr as $k = = $v) {$curlPost = $v. '? f= incoming parameters '; $ch = Curl_init ($curlPost); curl_ Setopt ($ch, Curlopt_returntransfer, true); Get Data return curl_setopt ($ch, Curlopt_binarytransfer, true);//Get Data back when Curlopt_returntransfer is enabled $data = Curl_ EXEC ($ch); Echo $k. ' # #: '. $data. '; Curl_close ($ch);
Second multithreaded 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, we found that at the same time request 4 different URLs, the processing speed of the loop seems faster than multi-threaded, the problem remains to be verified