简体   繁体   English

uzz-访问服务器上的代码错误

[英]Guzzle - Access to code on server error

I connecting to API by Guzzle: 我通过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 );
}

And it's working but when it's 'catch', I need to get code from response but I getting: 它正在工作,但是当它“捕获”时,我需要从响应中获取代码,但我得到:

Server error: `POST XXXXX` resulted in a `555 Error` response: {"status":"ERROR","errors":[{"message":"Subscribers already exists in this subscribers list","code":1304}]}

And I can't get the code. 而且我无法获取代码。 How to do this? 这个怎么做?

UPDATE UPDATE
Here is screen with full response. 这是带有完整响应的屏幕。
在此处输入图片说明

Just extract the response from the exception. 只需从异常中提取响应即可。 Guzzle throws a special BadResponseException in case of failure, so take a look at this class. 失败时,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM