Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-curl\u multi\u exec永远不会完成_Php_Curl - Fatal编程技术网

PHP-curl\u multi\u exec永远不会完成

PHP-curl\u multi\u exec永远不会完成,php,curl,Php,Curl,我在网上试过很多使用的例子。不幸的是,他们中没有一个对我“有效”,因为他们永远封锁,我从来没有得到回应 例1: 例2: 我试图修改一些示例,以便在我得到一个-1响应而没有效果时(除了停止CPU运行到100%)它们将休眠。我在CLI中进行了尝试,并作为Web服务器运行,结果相同 问题:这些脚本是否适用于其他人,或者他们是否需要修改/更新PHP7.0?也许我需要安装php7.0-curl以外的软件包 环境 我正在Ubuntu 16.04上运行PHP7.0: PHP 7.0.18-0ubuntu0

我在网上试过很多使用的例子。不幸的是,他们中没有一个对我“有效”,因为他们永远封锁,我从来没有得到回应

  • 例1:
  • 例2:
我试图修改一些示例,以便在我得到一个-1响应而没有效果时(除了停止CPU运行到100%)它们将休眠。我在CLI中进行了尝试,并作为Web服务器运行,结果相同

问题:这些脚本是否适用于其他人,或者他们是否需要修改/更新PHP7.0?也许我需要安装php7.0-curl以外的软件包

环境 我正在Ubuntu 16.04上运行PHP7.0:

PHP 7.0.18-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.18-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies
正如在上的评论所说,官方文件上有一个问题。示例2的类应该是这样的。更改了第一个while循环

class ParallelGet
{
  function __construct($urls)
  {
    // Create get requests for each URL
    $mh = curl_multi_init();
    foreach($urls as $i => $url)
    {
      $ch[$i] = curl_init($url);
      curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
      curl_multi_add_handle($mh, $ch[$i]);
    }

    // Start performing the request
    do {
        $execReturnValue = curl_multi_exec($mh, $runningHandles);
    } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);

    // Loop and continue processing the request
    while ($runningHandles && $execReturnValue == CURLM_OK) {

      // !!!!! changed this if and the next do-while !!!!!

      if (curl_multi_select($mh) != -1) {
        usleep(100);
      }

      do {
        $execReturnValue = curl_multi_exec($mh, $runningHandles);
      } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);

    }

    // Check for any errors
    if ($execReturnValue != CURLM_OK) {
      trigger_error("Curl multi read error $execReturnValue\n", E_USER_WARNING);
    }

    // Extract the content
    foreach($urls as $i => $url)
    {
      // Check for errors
      $curlError = curl_error($ch[$i]);
      if($curlError == "") {
        $res[$i] = curl_multi_getcontent($ch[$i]);
      } else {
        print "Curl error on handle $i: $curlError\n";
      }
      // Remove and close the handle
      curl_multi_remove_handle($mh, $ch[$i]);
      curl_close($ch[$i]);
    }
    // Clean up the curl_multi handle
    curl_multi_close($mh);

    // Print the response data
    print_r($res);
  }
}

你已经回答了我的问题,因为我张贴了它,所以我标记这是完整的答案。我想我的下一个问题属于另一个问题,但您是否知道为什么它们不是异步发送的,而是在队列中处理的。例如,尝试运行以下代码在运行带有睡眠(10)的localhost Web服务器后,您将看到在第一个请求的10秒钟过去之前,您不会得到google或yahoo结果。另外,如果只使用多个localhost请求,则所需时间将达到请求数x 10秒。很抱歉,我不太明白出了什么问题。我建议问一个新问题,我会找到的。谢谢,再多调查一点就可以了。我发现我在测试时出了问题。全部排序:)