《PHP用CURL多線程并發(fā)采集》要點(diǎn):
本文介紹了PHP用CURL多線程并發(fā)采集,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:PHP開發(fā)
CURL多線程并發(fā)采集一個(gè)案例(本站注:其實(shí)不是真正的多線程):
$urls = [ 'http://m.doudou360.com/bus/i/live.ashx?lid=1132', 'http://m.doudou360.com/bus/i/live.ashx?lid=5491', 'http://m.doudou360.com/bus/i/live.ashx?lid=4501', 'http://m.doudou360.com/bus/i/live.ashx?lid=4531', 'http://m.doudou360.com/bus/i/live.ashx?lid=1131', 'http://m.doudou360.com/bus/i/live.ashx?lid=5492', 'http://m.doudou360.com/bus/i/live.ashx?lid=4502', 'http://m.doudou360.com/bus/i/live.ashx?lid=4532', ]; $mh = curl_multi_init(); //1.初始化 $ch = []; foreach ( $urls as $i => $url ) { $ch[$i] = curl_init($url); curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch[$i], CURLOPT_TIMEOUT, 5); curl_setopt($ch[$i], CURLOPT_HTTPHEADER, ['Cookie:CurAreaCode=xiamen']); curl_multi_add_handle($mh, $ch[$i]); //2.循環(huán)增加ch句柄到批處理會(huì)話mh } /* 下面注釋代碼是網(wǎng)上相關(guān)文章較多使用的一種curl_multi_exec執(zhí)行方式: do { curl_multi_exec($mh, $active); } while ( $active ); DO-WHILE在整個(gè)URL請(qǐng)求期內(nèi)是死循環(huán),當(dāng)采集較多URL時(shí)容易導(dǎo)致CPU占用100%,不推薦使用! */ //推薦寫法 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); //3.運(yùn)行當(dāng)前cURL句柄的子連接 } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } foreach ( $urls as $i => $url ) { $data[] = curl_multi_getcontent($ch[$i]); //4.采集數(shù)據(jù) curl_multi_remove_handle($mh, $ch[$i]); //5.移除句柄資源 curl_close($ch[$i]); //6.關(guān)閉cURL會(huì)話 } curl_multi_close($mh); //7.關(guān)閉一組cURL句柄 print_r($data);
API返回的是json數(shù)據(jù),數(shù)據(jù)格式:
Array ( [success] => 1 [message] => [result] => Array ( [name] => 1路 [cur] => Array ( [lid] => 1182 //線路ID [sid] => 1155 [dire] => 下行 [time] => Array ( [0] => 05:50 //首班時(shí)間 [1] => 01:00 //末班時(shí)間 ) ) [oppo] => Array ( [lid] => 1181 //反向線路ID [sid] => 1154 ) [stas] => Array ( [0] => 火車站小廣場(chǎng) [1] => 梧村車站 [2] => 金榜公園 [3] => 文灶 [4] => 后江埭 [5] => 二市 [6] => 斗西路口 [7] => 眼科醫(yī)院 [8] => 中華城 [9] => 鎮(zhèn)海路 [10] => 大生里 [11] => 博物館 [12] => 理工學(xué)院(思明) [13] => 廈大 ) [live] => Array //實(shí)時(shí)公交信息 ( [0] => Array ( [0] => 6 //公交位置 [1] => 0 //公交狀態(tài) 0:到達(dá) 1:開往 [2] => 2 //公交數(shù)量 ) [1] => Array ( [0] => 13 [1] => 1 [2] => 1 ) ) [next] => 22:30 //下一班公交發(fā)車時(shí)間 ) )
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/33.html