我在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