我在PHP中使用GoCardless's API版本來處理我網站上的付款。但是,當他們的API返回錯誤時,我想向用戶顯示更有效的錯誤。檢索數組中的錯誤消息
我有一半的方式有,但我不知道是否有無論如何,我可以做到以下幾點:
如果我有以下錯誤:
Array ([error] => Array ([0] => The resource has already been confirmed))
反正是有隻提取部分與PHP?
我的代碼:
try{
$confirmed_resource = GoCardless::confirm_resource($confirm_params);
}catch(GoCardless_ApiException $e){
$err = 1;
print '<h2>Payment Error</h2>
<p>Server Returned : <code>' . $e->getMessage() . '</code></p>';
}
感謝。
UPDATE 1:觸發異常
代碼:
$http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_response_code < 200 || $http_response_code > 300) {
// Create a string
$message = print_r(json_decode($result, true), true);
// Throw an exception with the error message
throw new GoCardless_ApiException($message, $http_response_code);
}
更新2: - >print_r($e->getMessage())
輸出:
Array ([error] => Array ([0] => The resource has already been confirmed))
'$ errorArray ['error'] [0]' – prodigitalson