2017-04-10 43 views
0

我連接到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}]} 

而我無法獲得代碼。這個怎麼做?

UPDATE
這裏是完整的響應屏幕。
enter image description here

+0

你說的是異常代碼嗎?或者HTTP返回狀態碼? –

+0

@AlexHowansky HTTP。這個代碼 - >'''「code」:1304''' – Vertisan

+1

你將不得不看看服務器上運行的任何軟件的文檔 – Jaime

回答

0

只是從異常中提取響應。在失敗的情況下,Guzzle會拋出一個特殊的BadResponseException,所以請看這堂課。

try { 
    // ... 
} catch (BadResponseException $exception) { 
    // 555 
    $exception->getCode(); 

    $appError = json_decode(
     $exception->getResponse()->getBody()->getContents(), 
     true 
    ); 
    // 1304 
    $appErrorCode = $appError['errors'][0]['code']; 
} 
+0

同樣的問題,我更新了與圖像問題。 – Vertisan

+0

最後要注意' - > getContents()'。我希望你的想法 - 例外包含響應,你可以像平常的響應一樣工作(提取JSON等)。 –