Web Service can response with a Fault in 2 occasions.

Fault send by the web service framework. (E.g. Invalid authentication, invalid signature found) Fault send by the user business logic.

There is a slightly difference in the content of SOAP 1.1 and SOAP 1.2. But they mainly contain the following elements.

Code – A code to represent the classification of the fault. Possible fault codes can be found, http://www.w3.org/TR/soap12-part1/#faultcodes Reason – A human readable details of the reason. Details – More information about the details, mostly supposed to be read by the client application. Role – Indicates which SOAP header caused the fault. This is very rarely used in faults send from the business logic.

Sending SOAP Faults

In WSF/PHP you have the WSFault class to deal with SOAP faults. You can send a fault in your service logic by throwing an instance of WSFault class like this.

/** * divide mathematical operation * @param int $dividend * (maps to xs:int) * @param int $divisor * (maps to xs:int) * @return float $result * (maps to xs:float) */ function divide ( $dividend , $divisor ) { // dividing from 0 is invalid, we wil *throw* fault in such cases.. if ( $divisor == 0 ) { throw new WSFault ( "Sender" , "dividing from 0 is invalid" ) ; } $result = ( float ) $dividend / $divisor ; return array ( "result" => $result ) ; }

Here I have throw an WSFault whenever I encounter my divisor is zero. And the WSF/PHP will take care of building the SOAP message according to the given version (default is to SOAP 1.2) and send back to the client.

Handling SOAP Faults

Similar to the service, client API also treat the SOAP fault as an instance of WSFault. So whenever you do a web service request, put inside try, catch block so you can catch exception in case of fault is received. Here is an example of handling fault while calling the divide operation in the above example.

// creating the client, we retrieved the wsdl from service url + ?wsdl $client = new WSClient ( array ( "wsdl" => "http://localhost/myblog/fault_service.php?wsdl" ) ) ; $proxy = $client -> getProxy ( ) ; try { // calling the operation $response = $proxy -> divide ( array ( "dividend" => 5 , "divisor" => 0 ) ) ; // printing the result echo $response [ "result" ] ; } catch ( Exception $e ) { // if the instance is WSFault we print the code and the reason if ( $e instanceof WSFault ) { printf ( "Soap Fault Reason: %s

" , $e -> Reason ) ; printf ( "Soap Fault Code: %s

" , $e -> Code ) ; } else { printf ( "Message = %s

" , $e -> getMessage ( ) ) ; } }

As you can see WSF/PHP covers the complexity of building and handling SOAP faults, Rather it gives you an API with the use of PHP Exception Construct that you already familiar with.