在抛出SoapFault时更改HTTP状态代码

时间:2015-06-18 12:41:43

标签: php soap soapserver

每当我在PHP应用程序中抛出SoapFault时,HTTP状态代码都会设置为HTTP/1.1 500 Internal Service Error。即使我在抛出http_response_code()之前使用header()SoapFault将状态代码设置为不同的状态,也会发生这种情况。投掷SoapFault时是否无法更改状态代码?并且不应该被称为HTTP/1.1 500 Internal Server Error?我不知道HTTP/1.1 500 Internal Service Error是否存在。

我使用SoapUI检查来自SoapServer的回复。

抛出SoapFault(在SoapServer对象处理的函数内)

的示例
http_response_code(404);
throw new SoapFault('Client', 'The specified item was not found');

回复示例:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body>
    <SOAP-ENV:Fault>
      <faultcode>SOAP-ENV:Client</faultcode>
      <faultstring>The specified item was not found</faultstring>
    </SOAP-ENV:Fault>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

示例响应标头:

Date    Thu, 18 Jun 2015 12:27:23 GMT
Content-Length  299
#status#    HTTP/1.1 500 Internal Service Error
Expires Thu, 19 Nov 1981 08:52:00 GMT
Set-Cookie  PHPSESSID=kqhubpja05jfcluohbgts8lmk6; path=/
Connection  close
Content-Type    text/xml; charset=utf-8
Server  Apache/2.4.9 (Win64) PHP/5.5.12
X-Powered-By    PHP/5.5.12
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache

1 个答案:

答案 0 :(得分:0)

我最终编写了自己的代码来输出肥皂故障。然后我可以返回我想要的任何HTTP状态代码。我不确定应该使用$faultname$headerfault。如果您有任何想法,请发表评论。

function soapFault($faultcode, $faultstring, $faultactor = null, $detail = null, $faultname = null, $headerfault = null) {
   switch($faultcode) {
      case 'Client':
      case 'Server':
      case 'VersionMismatch':
      case 'MustUnderstand':
         $faultcode = 'SOAP-ENV:'.$faultcode;
   }

   die(
<<<FAULT
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>$faultcode</faultcode>
         <faultstring>$faultstring</faultstring>
         <faultactor>$faultactor</faultactor>
         <detail>$detail</detail>
     </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
FAULT
   );
}