?php
$ch = array();
$res = array();
$conn = array();
$urls = array(
'baidu' => "http://www.baidu.com/",
'cheyun' => "http://auto.jrj.com.cn/",
'w3c' => "http://www.w3cschool.cc/",
);
// 創(chuàng)建批處理cURL句柄
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
// 創(chuàng)建一對(duì)cURL資源
$conn[$i] = curl_init();
// 設(shè)置URL和相應(yīng)的選項(xiàng)
curl_setopt($conn[$i], CURLOPT_URL, $url);
curl_setopt($conn[$i], CURLOPT_HEADER, 0);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($conn[$i], CURLOPT_TIMEOUT, 10);
//302跳轉(zhuǎn)
curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1);
// 增加句柄
curl_multi_add_handle($mh, $conn[$i]);
}
$active = null;
//防卡死寫法:執(zhí)行批處理句柄
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);
}
}
foreach ($urls as $i => $url) {
//獲取當(dāng)前解析的cURL的相關(guān)傳輸信息
$info = curl_multi_info_read($mh);
//獲取請(qǐng)求頭信息
$heards = curl_getinfo($conn[$i]);
var_dump($heards);
//獲取輸出的文本流
$res[$i] = curl_multi_getcontent($conn[$i]);
// 移除curl批處理句柄資源中的某個(gè)句柄資源
curl_multi_remove_handle($mh, $conn[$i]);
//關(guān)閉cURL會(huì)話
curl_close($conn[$i]);
}
//關(guān)閉全部句柄
curl_multi_close($mh);
//var_dump($res);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》及《PHP運(yùn)算與運(yùn)算符用法總結(jié)》