【问题标题】:PHP Guzzle: Empty body responsePHP Guzzle:空体响应
【发布时间】:2017-10-04 07:01:33
【问题描述】:

我刚开始尝试 guzzle,但我在响应正文上得到一个空字符串

    $client = new Client([
      'base_uri' => 'http://httpbin.org',
      'timeout'  => 2.0,
   ]);
   $response = $client->request('GET', '', ['debug' => true]);
   var_dump($response->getBody()->getContents());

我得到的回应是:

* About to connect() to httpbin.org port 80 (#0)
*   Trying 23.22.14.18...
* Connected to httpbin.org (23.22.14.18) port 80 (#0)
> GET / HTTP/1.1
User-Agent: GuzzleHttp/6.2.0 curl/7.29.0 PHP/5.5.34
Host: httpbin.org

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 06 Jun 2016 06:48:13 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12150
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

* Connection #0 to host httpbin.org left intact
string(0) ""

如您所见,正文是“”,但 Content-Length 是正确的。

【问题讨论】:

    标签: php guzzle


    【解决方案1】:

    guzzlehttp/psr7 的主分支上存在错误提交。如果您将版本锁定在 composer.json 文件中,那么一切都应该按预期工作。

    "guzzlehttp/guzzle": "6.2",
    "guzzlehttp/psr7": "1.3"
    

    【讨论】:

    • 如果您最近在 php7 构建上安装了 guzzle,您将遇到 json 解析错误,因为它在抽象层返回空字符串。这让我至少头痛了 4 个小时才找到这个!
    猜你喜欢
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2016-05-12
    • 2021-03-20
    • 2018-11-23
    • 2018-05-29
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多