![](/img/trans.png)
[英]Facing error in changing a guzzle code from guzzle 5 to guzzle 6
[英]Guzzle - Access to code on server error
我通过Guzzle连接到API:
$client = new Client();
try {
$res = $client->post( 'xxx' . $this->url , [
'headers' => $headers,
'json' => $data,
]);
} catch( Exception $e ) {
echo json_decode( $e->getResponse()->getBody(), true );
}
它正在工作,但是当它“捕获”时,我需要从响应中获取代码,但我得到:
Server error: `POST XXXXX` resulted in a `555 Error` response: {"status":"ERROR","errors":[{"message":"Subscribers already exists in this subscribers list","code":1304}]}
而且我无法获取代码。 这个怎么做?
只需从异常中提取响应即可。 失败时,Guzzle会抛出一个特殊的BadResponseException
,因此请看一下此类。
try {
// ...
} catch (BadResponseException $exception) {
// 555
$exception->getCode();
$appError = json_decode(
$exception->getResponse()->getBody()->getContents(),
true
);
// 1304
$appErrorCode = $appError['errors'][0]['code'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.