我正在尝试返回一个 HTTP 标头和 XML,说明一个过程是否成功。
在开发我的 API 时,这一切似乎都运行良好,我正在从同一服务器对 API 进行测试调用,并将相关的标头状态代码与 XML 一起获取。但是,当我再次测试它但从其他服务器调用 API 并遇到问题时。
如果响应是 200,那么我会得到响应 XML。如果有错误,我会收到标头状态代码 OK,但不会收到错误 XML。我玩过错误消息功能,并注意到如果我注释掉header($status_header);
错误 XML 被发送。任何人都可以阐明为什么会发生这种情况吗?
static function errorResponse($status = 400, $body = '') {
$status_message = APIResponse::getStatusCodeMessage($status);
$status_header = 'HTTP/1.1 ' . $status . ' ' . $status_message;
// set the HTTP status
header($status_header);
if($body == '') {
// create the message if none is passed
$body = '';
switch($status)
{
case 401:
$body = 'You must be authorized to view this page.';
break;
case 404:
$body = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.';
break;
case 500:
$body = 'The server encountered an error processing your request.';
break;
case 501:
$body = 'The requested method is not implemented.';
break;
}
}
$response_xml = '<?xml version="1.0" encoding="utf-8"?>
<error>
<date>' . date("D, d M Y H:i:s", time()) . '</date>
<status>' . $status . ' - ' . $status_message . '</status>
<message><![CDATA[' . $body . ']]></message>
</error>';
return $response_xml;
exit;
}