PHP - REST API - HTTP头 - 错误处理 [英] PHP - REST API - http headers - error handling

查看:148
本文介绍了PHP - REST API - HTTP头 - 错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用XML声明的过程是否成功与否一起返回一个HTTP标头。

本都出现,而发展我的API是工作的罚款,我正在测试呼叫从同一个服务器的API,并与XML获得相关的头状态code一起。然而,当我再次测试这一点,但调用其他服务器的API和我遇到的一个问题。

如果响应是200然后我得到的响应XML。如果有错误,我收到了标题状态code OK,但得到没有错误XML。我身边有机智的错误信息功能发挥,并注意到,如果我注释掉头($ status_header); 错误XML发送。任何人都可以提供一些线索,为什么这可能发生?

 静态函数errorResponse($状态= 400,$ =身体''){    $ STATUS_MESSAGE = APIResponse的getStatus :: codeMessage派生($状态);
            $ status_header =HTTP / 1.1。 $状态。 ''。 $ STATUS_MESSAGE;    //设置HTTP状态
    头($ status_header);    如果($体==''){
       //如果没有传递创建消息
      $身体='';      开关($状态)
      {
          案例401:
              $身体='必须被授权查看该页。';
              打破;
          案例404:
              $身体='请求的URL。 $ _ SERVER ['REQUEST_URI']。 ' 没找到。';
              打破;
          案例500:
              $身体='服务器遇到一个错误处理您的请求。;
              打破;
          案例501:
              $身体='请求的方法未实现。;
              打破;
      }    }      $ response_xml ='< XML版本=1.0编码=UTF-8&GT?;
          <错误>
              <日期和GT;' 。日期(D,D M Y H:我:秒,时间())。 '< /日期和GT;
              <状态>' 。 $状态。 - 。 $ STATUS_MESSAGE。 '< /状态>
              <&消息GT;<![CDATA [。 $身体。 ']]>< /消息>
          < /错误>';      返回$ response_xml;
      出口;}


解决方案

现在我已经整理这一点,这是我的包含类卷曲功能的错误,无关此类。

I am trying to return a HTTP header together with XML stating whether a process has been successful or not.

This all appeared to be working fine whilst developing my API, I was making test calls to the API from the same server and getting the relevant header status code together with the XML. However, when I test this again but calling the API from other server and am encountering a problem.

If the response is 200 then I get the response XML. If there is an error, I receive the header status code OK but get no error XML. I have played around wit the the error messaging function and noticed that if I comment out header($status_header); the error XML is sent. Can anyone shed some light as to why this might be happening?

    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;  

}  

解决方案

I've now sorted this, it was an error with my class containing CURL functions, nothing to do with this class.

这篇关于PHP - REST API - HTTP头 - 错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆