【问题标题】:curl_multi_exec fails on Mac OS Xcurl_multi_exec 在 Mac OS X 上失败
【发布时间】:2013-11-28 17:56:03
【问题描述】:

我正在尝试在我的 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 已安装,因为我的常规 curl“单个”请求运行良好。

我认为这是 Mac OS 的问题,但我找不到任何解决方法。你有什么想法吗?

编辑:我在 Linux 服务器上尝试了相同的代码,一切正常。

【问题讨论】:

  • 尝试使用set_time_limit(0)改变时间限制;//不适合生产现场,仅供测试使用。
  • 它只是运行,运行,运行,但没有任何反应......

标签: php macos curl curl-multi


【解决方案1】:

在 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 了解更多信息。

【讨论】:

猜你喜欢
  • 2013-02-12
  • 1970-01-01
  • 2012-06-09
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 2011-04-23
相关资源
最近更新 更多