簡體   English   中英

PHP-curl_multi_exec永遠不會完成

[英]PHP - curl_multi_exec never completes

我已經嘗試了許多使用curl_multi_exec的在線示例。 不幸的是,他們都沒有為我“工作”,因為他們永遠阻止了我,我也從未得到回應。

我嘗試修改一些示例,以使它們在我收到沒有響應的-1響應時(除了將我的CPU停止為100%停止運行)都可以進入睡眠狀態。 我在CLI中嘗試過,並作為Web服務器運行,結果相同。

問題:這些腳本是否對其他人有用,還是需要針對PHP 7.0進行修改/更新? 也許我需要安裝除php7.0-curl以外的軟件包?

環境

我在Ubuntu 16.04上運行PHP 7.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

正如http://php.net/manual/en/function.curl-multi-init.php#115055上的評論所說,官方文檔中存在問題。 因此,示例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);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM