PHP需要同时批量请求多个URL时,可以使用curl_multi实现并发请求。
public static function rolling_curl($urls, $postData=array(),$custom_options = null){//多个url访问
if (sizeof($urls)==0) return;
// make sure the rolling window isn't greater than the # of urls
$rolling_window = 8;
$rolling_window = (sizeof($urls) < $rolling_window) ? sizeof($urls) : $rolling_window;
$master = curl_multi_init();
$curl_arr = array();
// add additional curl options here
$std_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 60
);
$std_options[CURLOPT_SSL_VERIFYPEER] = FALSE;
$std_options[CURLOPT_SSL_VERIFYHOST] = FALSE;
$std_options[CURLOPT_HTTPHEADER] =array(
'Content-Type: application/json'
);
$options = ($custom_options) ? ($std_options + $custom_options) : $std_options;
// start the first batch批 of requests
for ($i = 0; $i < $rolling_window; $i++) {
$ch = curl_init();
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = json_encode($postData[$i]);
$options[CURLOPT_URL] = $urls[$i];
curl_setopt_array($ch, $options);
curl_multi_add_handle($master, $ch); //添加对象
}
do {
while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM); //执行
if ($execrun != CURLM_OK) {
break;
}
// a request was just completed -- find out which one
while ($done = curl_multi_info_read($master)) {
$info = curl_getinfo($done['handle']);
if ($info['http_code'] == 200) {
$output = curl_multi_getcontent($done['handle']); //获取结果
// request successful. process output using the callback function.
//这里写成功后的操作
// 把请求已经完成了得 curl handle 删除
curl_multi_remove_handle($master, $done['handle']);
// start a new request (it's important to do this before removing the old one)
}
if($i<sizeof($urls)){
$ch = curl_init();
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = json_encode($postData[$i]);
$options[CURLOPT_URL] = $urls[$i++]; // increment i
curl_setopt_array($ch, $options);
curl_multi_add_handle($master, $ch);
}
// remove the curl handle that just completed
curl_multi_remove_handle($master, $done['handle']);
} //while
// 当没有数据的时候进行堵塞,把 CPU 使用权交出来,避免上面 do 死循环空跑数据导致 CPU 100%
if ($running) {
$rel = curl_multi_select($master, 1);
if($rel == -1){
usleep(1000);
}
}
} while ($running);
curl_multi_close($master);
return true;
}