PHP Curl多執行緒原理例項詳解
<?php
// 建立一對cURL資源
$ch1 = curl_init();
$ch2 = curl_init();
// 設定URL和相應的選項
curl_setopt($ch1, CURLOPT_URL, "http://www.jb51.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
// 建立批處理cURL控制代碼
$mh = curl_multi_init();
// 增加2個控制代碼
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
// 執行批處理控制代碼
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);
}
}
// 關閉全部控制代碼
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>