for ($i=0; $i < 100; $i++){ $urls[] = "http://blog.csdn.net/lg_lin?wd=".mt_rand(10000,20000);}$t = microtime(true);//單線程foreach ($urlsas$key => $value){ $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_URL, $value); $response = curl_exec($ch); curl_close($ch); $result_new[$key] = $response;}var_dump($result_new);$e = microtime(true);echo"For迴圈:".($e-$t)."\n";/** **** 多線程**** * 由於受網路頻寬節流設定,一次性不能並發太多,可以分批處理 * 總量超過100以後,效果就比單線程明顯提高*/$p = 5;//每次執行多少條$ring = ceil(count($urls) / $p);for($n = 0; $n < $ring; $n++){ $temp_url = array(); $star = $n * $p; $end = ($n+1) * $p; for($ii = $star; $ii < $end; $ii++) { if (isset($urls[$ii])) { $temp_url[] = $urls[$ii]; } } $mh = curl_multi_init(); foreach ($temp_urlas$i => $url) { $conn[$i] = curl_init($url); curl_setopt($conn[$i], CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($conn[$i], CURLOPT_TIMEOUT, 30); curl_setopt($conn[$i], CURLOPT_URL, $url); curl_multi_add_handle($mh,$conn[$i]); } do{ curl_multi_exec($mh, $active); } while ($active); $active = null; foreach ($temp_urlas$i => $url) { $data[$i] = curl_multi_getcontent($conn[$i]); // 獲得爬取的代碼字串 } foreach ($temp_urlas$i => $url) { curl_multi_remove_handle($mh, $conn[$i]); curl_close($conn[$i]); } curl_multi_close($mh); var_dump($data);}$t = microtime(true);echo"多線程:".($t-$e)."\n";exit();
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
以上就介紹了利用curl_multi_init並行多執行緒網路資料,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。