我无法使用 php 的 curl_multi_exec 创建多个 xml 请求。
问题是包含 curl_multi_exec 命令的 do...while 循环只运行一次然后退出。
使用的资源:
http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/ http://php.net/manual/en/function.curl-multi-exec.php/ http:// www.rustyrazorblade.com/2008/02/curl_multi_exec/
看看我的代码:
//Multi handle curl initialization
$mh = curl_multi_init();
//set url
$url = 'my_url';
foreach($latLng as $id => $l) {
$ch[$id] = curl_init();
//$request previously set
//Initialize and set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//add to multi_handle
curl_multi_add_handle($mh, $ch[$id]);
}
//Execute the handles
$running = null;
do {
$mrc = curl_multi_exec($mh, $running);
$ready=curl_multi_select($mh);
echo "Ran once\n";
} while ($mrc == CURLM_CALL_MULTI_PERFORM && $ready > 0);
while ($active && $mrc == CURLM_OK) {
if ($curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $running);
echo "Ran again\n";
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($mh as $c) {
// HTTP response code
$code = curl_getinfo($c, CURLINFO_HTTP_CODE);
// cURL error number
$curl_errno = curl_errno($c);
// cURL error message
$curl_error = curl_error($c);
// output if there was an error
if ($curl_error) {
echo("*** cURL error: ($curl_errno) $curl_error\n");
}
}
//get content and remove handles
foreach ($ch as $c) {
$result[] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
print_r($result);
//Close curl
curl_multi_close($mh);
}
我知道请求是有效的,因为我在执行单次 curl 执行时收到了正确的返回数据。问题在于 curl_multi_exec()。
我收到的输出是“运行一次”,后跟 curl_multi_getcontent() 调用的空数组。见下文:
跑一次数组( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => )
任何帮助是极大的赞赏。