【问题标题】:Getting fatal error on sending multiple requests with Guzzle?使用 Guzzle 发送多个请求时出现致命错误?
【发布时间】:2015-04-08 01:38:13
【问题描述】:

这是我得到的错误

Argument 1 passed to GuzzleHttp\Client::send() must implement interface GuzzleHttp\Message\RequestInterface, array given,

这是我正在使用的代码

$guzzleResponses = $client->send(array(
$client->get('http://www.sitepoint.com/'),
$client->get('http://www.sitepoint.com/'),
$client->get('http://www.sitepoint.com/')
));

foreach($guzzleResponses as $guzzleResponse) {
$goutteObject = new Symfony\Component\BrowserKit\Response(
       $guzzleResponse->getBody(true), 
       $guzzleResponse->getStatusCode(), 
       $guzzleResponse->getHeaders()
);
}

我理解这个错误意味着当它期望其他东西时我正在传递一个数组。但是我想同时处理多个请求,想不出别的办法?

【问题讨论】:

    标签: php guzzle


    【解决方案1】:

    这个问题已经被(你自己)问过并回答了here。要异步发送多个请求,您必须使用GuzzleHttp\Pool

    您对GuzzleHttp\Client->send() 的呼叫不正确。您实际上是在为它提供一组 ResponseInterfaces(不是预期的)。 GuzzleHttp\Client->get() 将在内部调用$this->sent($this->createRequest('GET', $url, $options)),这意味着它将实际执行http 请求并返回GuzzleHttp\Message\ResponseInterface 的实例。

    【讨论】:

      【解决方案2】:

      根据其他几个同样需要的问题,使用新的 Guzzlehttp 包 guzzlehttp/guzzle 可以在 here 找到答案。点击链接获取完整描述或查看下面的示例

      查看示例这里

          $newClient = new  \GuzzleHttp\Client(['base_uri' => $base]);
          foreach($documents->documents as $doc){
      
              $params = [
                  'language' =>'eng',
                  'text' => $doc->summary,
                  'apikey' => $key
              ];
      
              $requestArr[$doc->reference] = $newClient->getAsync( '/1/api/sync/analyze/v1?' . http_build_query( $params) );
          }
      
          $responses = \GuzzleHttp\Promise\unwrap($requestArr); //$newClient->send( $requestArr );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 2017-08-28
        • 2020-02-07
        相关资源
        最近更新 更多