我正在尝试从我的Mac OS X(Mavericks)下的 php.net 运行这段简单的代码来尝试cURL多执行功能:
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$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
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
这不起作用,因为我得到的唯一输出是:
PHP 致命错误:超过 30 秒的最大执行时间
这是我的环境:Mac OS X 10.9,PHP 5.4.17,Apache 2.2.24。
cUrl 已安装,因为我的常规卷曲"单个"请求效果很好。
我认为这是Mac OS的问题,但我找不到任何修复程序。你有什么想法吗?
编辑:我在Linux服务器上尝试了相同的代码,一切正常。
在 php 5.3.18+ 上,请注意 curl_multi_select() 可能会永远返回 -1,直到您调用 curl_multi_exec()。
试试这个:
while ($this->active && $mrc == CURLM_OK)
{
// add this line
while (curl_multi_exec($this->mh, $this->active) === CURLM_CALL_MULTI_PERFORM);
if (curl_multi_select($this->mh) != -1)
{
do {
$mrc = curl_multi_exec($this->mh, $this->active);
if ($mrc == CURLM_OK)
{
while($info = curl_multi_info_read($this->mh))
{
$this->process($info);
}
}
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
有关详细信息,请参阅 https://bugs.php.net/bug.php?id=63411 或 http://marchtea.com/?p=109。