0
我在回傳和處理應用程序中的錯誤時遇到了困難。該應用程序基於PHP(版本5.3.29)。PHP:通過HTTP POST返回錯誤並在CURL中引用他們
我有一個頁面TakePayment.php其具有如下的結構:
try
{
if(<<some condition>>)
{
<<log error>>
header("HTTP/1.1 500 Internal Server Error");
exit;
}
<<take payment>>
//if payment was ok, emit and empty json string
Header('Content-Type: application/json');
echo "{}";
}
catch(Exception $e)
{
<<log error>>
header("HTTP/1.1 500 Internal Server Error");
exit;
}
我有另一頁CancelAppointment.php其具有以下的代碼:
$ch = curl_init();
$timeout = 5;
$url = 'https://############/TakePayment.php';
$formData = array('isCancellationPayment' => '1', 'booking_id' => $bookingId);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formData));
$data = curl_exec($ch);
if(!curl_errno($ch))
{
?>
<p>Appointment has been cancelled </p>
<?
}
else
{
echo 'Fatal error taking payment, please contact PsychFinder support with the following information: ' . curl_error($ch);
exit;
}
curl_close($ch);
的我遇到的問題是if(!curl_errno($ ch))塊沒有選擇TakePayment.php頁面返回500狀態碼並輸出
預約已被取消
我在這裏做錯了什麼,無論是在返回500錯誤還是用CURL捕獲它。
當我使用客戶端Ajax(在應用程序的另一部分)調用相同的頁面時,它按預期工作,並且如果存在付款問題會顯示錯誤,所以這讓我認爲我的CURL使用是錯誤的?
$.ajax({
url: "/TakePayment.php",
method: 'POST',
data: {'booking_id': jsonData["bookingid"]},
dataType: 'html',
cache: false,
success: function(data)
{
},
error: function(jqXHR, textStatus, errorThrown)
{
document.write("Error taking payment for session. Please contact Support: " + errorThrown);
throw new Error();
}
});
在此先感謝。
謝謝布萊恩,這個伎倆。 –