正如@Brad curl-multi-exec 所说,应该是一个选项。
http://php.net/manual/en/function.curl-multi-exec.php
<?php
//create the multiple cURL handle
$mh = curl_multi_init();
// create both cURL resources
for($i = 0; $i < 10;$i++){
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_URL, "http://urhost//path/to/process_a.php");
curl_setopt($ch[$i], CURLOPT_HEADER, 0);
curl_multi_add_handle($mh,$ch[$i]);//add the handles
}
$active = null;
//execute the handles
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);
}
}
//close the handles
for($i = 0; $i < 10;$i++){
curl_multi_remove_handle($mh, $ch[$i]);
}
curl_multi_close($mh);
?>
我通过调用下面的另一个脚本来测试这个脚本:
<?php
print microtime();//Return current Unix timestamp with microseconds
print '<br>';
?>
这是结果,每个句柄的执行时间相差微秒。
0.27085300 1340214659
0.44853600 1340214659
0.46611800 1340214659
0.48201000 1340214659
0.50209400 1340214659
0.48233900 1340214659
0.52274300 1340214659
0.54757800 1340214659
0.57316900 1340214659
0.59475800 1340214659