0

所以我有以下代码

    try {
        $response = $client->request('POST', 'http://dev.api.example.com/v1/partners', [
            // params
        ]);
    } catch (ClientException $ex) {
        Debug::dump($ex);
        die;
    }

现在我故意发送错误数据进行测试,并且 api 发送 400 代码,因为缺少某些内容,我正在使用 try 和 catch 块捕获它。现在我想展示 api 返回的主体。我试过以下

$ex->getResponse()->getBody()

但它返回的只是以下内容。

GuzzleHttp\Psr7\Stream Object
(
    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #73
    [size:GuzzleHttp\Psr7\Stream:private] => 
    [seekable:GuzzleHttp\Psr7\Stream:private] => 1
    [readable:GuzzleHttp\Psr7\Stream:private] => 1
    [writable:GuzzleHttp\Psr7\Stream:private] => 1
    [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
    [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
        (
        )

)

虽然 api 在邮寄中发送这个

{
  "success": false,
  "error": {
    "code": 400,
    "message": "The name has already been taken.<br />The email field is required."
  }
}
4

1 回答 1

1

好的,我已经想通了。您需要做的就是在异常中执行以下操作。

$ex->getResponse()->getBody()->getContents()

作为

http://docs.guzzlephp.org/en/latest/psr7.html

于 2015-09-17T14:30:38.750 回答