【问题标题】:Facing error in changing a guzzle code from guzzle 5 to guzzle 6将 guzzle 代码从 guzzle 5 更改为 guzzle 6 时面临错误
【发布时间】:2019-08-26 02:45:32
【问题描述】:

GuzzleHttp\Client::send() 必须实现接口 Psr\Http\Message\RequestInterface,给出 GuzzleHttp\Psr7\Response 的实例

尝试过修改,但没有成功。

$request = $client->request('GET', $url, [
  'timeout' => 15,
  'exceptions' => false,
  'keepalive' => true
]);

$response = $client->send($request);
$body = $response->getBody();
$content = $body ? $body->getContents() : '';
$code = $response->getStatusCode(); 

我希望发送请求能够成功发送。然而,由于 guzzle 6 的变化,它没有按预期工作。

【问题讨论】:

  • 没有按预期工作是什么意思?您是否收到任何错误或您没有得到预期的响应?请具体。

标签: php guzzle guzzle6


【解决方案1】:

您不需要手动调用->send(),实际请求已经在->request() 内部完成。这就是为什么您会在错误中看到 GuzzleHttp\Psr7\Response

所以只需删除发送行就完成了。

$response = $client->request('GET', $url, [
  'timeout' => 15,
  'exceptions' => false,
  'keepalive' => true
]);

$body = $response->getBody();
$content = $body ? $body->getContents() : '';
$code = $response->getStatusCode();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多