PHP - REST API - http headers - error handling


PHP - REST API - http headers - error handling

我正在尝试返回一个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;  
}  

我现在已经对此进行了排序,这是我的类包含 CURL 函数的错误,与此类无关。