The SoapFault class

(PHP 5 >= 5.0.1)


Represents a SOAP fault.

Class synopsis

SoapFault extends Exception {
/* Inherited properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
__construct ( string $faultcode , string $faultstring [, string $faultactor [, string $detail [, string $faultname [, string $headerfault ]]]] )
SoapFault ( string $faultcode , string $faultstring [, string $faultactor [, string $detail [, string $faultname [, string $headerfault ]]]] )
public string __toString ( void )
/* Inherited methods */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )

Table of Contents

add a note add a note

User Contributed Notes 3 notes

dmitry dot koterov at gmail dot com
6 years ago
You may use undocumented and invisible property $e->faultcode to access string version of $code. Because standard $e->getCode() does not work:

= new SoapFault("test", "msg");
var_dump($e->getCode()); // prints "0"
var_dump($e->faultcode); // prints "test"

Also you may use namespaced fault codes:

= new SoapFault(array("namespace", "test"), "msg");

- see ext/soap/soap.php, PHP_METHOD(SoapFault, SoapFault). To access the namespace, use $e->faultcodens
chris AT cmbuckley DOT co DOT uk
5 years ago
A bit more digging in ext/soap/soap.c and the set_soap_fault function reveals the other undocumented properties from the constructor:

try {
    throw new
SoapFault('code', 'string', 'actor', 'detail', 'name', 'header');
} catch (
Exception $ex) {
var_dump($ex->faultcode, $ex->faultstring, $ex->faultactor, $ex->detail, $ex->_name, $ex->headerfault);
fbernoldi at gmail dot com
3 years ago
Hi all,

I've decided to post this since it may be helpful, I've spend a couple of days trying to do this.

In order to use wsdl's specified faults with complex types, i.e:

WSDL definitions:

(xsd:schema namespace, ns1 = target namespace)

<xsd:element name="doubleFault">
      <xsd:element name="detail1" type="xsd:string"/>
      <xsd:element name="detail2" type="xsd:string"/>

WSDL messages:

<message name="fault_specified">
    <part name="relevant_name" element="ns1:doubleFault"/>

WSDL port type:

<portType name="test">
  <operation name="operationTest">
  <input message="ns1:not_relevant_request"/>
  <output message="ns1:not_relevant_response"/>
  <fault name="FaultSpecified" message="ns1:fault_specified"/>

You have to specify the response in the detail parameter as an array corresponding the tag names.

PHP Code:


function operationTest($request_param ...) {

// ...
$array_details = array("detail1" => "Explanation 1", "detail2" => "Explanation 2");
    return new
SoapFault("Server", "example fault string", null, $array_details, "FaultSpecified");


$server = new SOAPServer("handmade.wsdl");


that should respond something like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="" xmlns:ns1="http://mynamespace">
         <faultstring>example fault string</faultstring>
               <detail1>Explanation 1</detail1>
               <detail2>Explanation 2</detail2>

I Hope it helps,
To Top