Changing HTTP Status Code when throwing SoapFault

1.6k Views Asked by At

Whenever I throw a SoapFault in my PHP applications, the HTTP Status Code is set to HTTP/1.1 500 Internal Service Error. This happens even if I set the status code to something different using http_response_code() or header() before throwing the SoapFault. Is there no way to change the status code when throwing a SoapFault? And shouldn't it be called HTTP/1.1 500 Internal Server Error? I didn't know HTTP/1.1 500 Internal Service Error even existed.

I'm using SoapUI to inspect the responses from my SoapServer.

Example of throwing SoapFault (inside a function handled by a SoapServer object)

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

Example response:

<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>

Example response headers:

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

There are 1 best solutions below

0
On BEST ANSWER

I ended up writing my own code for outputting a soap fault. Then I can return whichever HTTP Status Code I want. I'm not sure what $faultname and $headerfault should be used for. Please leave a comment if you've got any idea.

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
   );
}