Catching SOAP Faults






Catching SOAP Faults

Problem

You want to handle a SOAP server returning an error in the form of a SOAP fault. This allows you to fail gracefully when there's a problem with your request or the service.

Solution

Wrap your code inside a try/catch block and check for a SOAPFault:

<?php
try {
    $wsdl_url =
        'http://www.example.com/TemperatureService.wsdl';

    $client = new SOAPClient($wsdl_url);

    $temp = $client->getTemp('New York'); // This should be a Zip Code
    print $temp;
} catch (SOAPFault $exception) {
    print $exception;
}
?>

Or configure your SOAPClient not to use exceptions, and check the return value of is_soap_fault( ):

<?php
$wsdl_url =
    'http://www.example.com/TemperatureService.wsdl';

// Disable exceptions
$opts = array('exceptions' => 0);
$client = new SOAPClient($wsdl_url, $opts);

$temp = $client->getTemp('New York'); // This should be a zip code
if (is_soap_fault($temp)) {
    print $exception;
} else {
    print $temp;
}
?>

Discussion

When a SOAP server generates an error, it returns a SOAP fault. This can be a mistake on your part, such as calling a method that doesn't exist or passing the incorrect number (or type) of parameters, or it can be a server error. For instance, the service lacks temperature information for a particular zip code, but for reasons external to your SOAP request.

The SOAP extension transforms SOAP faults into PHP exceptions, as shown in Figure.

Detecting SOAP faults with exceptions

<?php
try {
    $wsdl_url =
        'http://www.example.com/TemperatureService.wsdl';

    $client = new SOAPClient($wsdl_url);

    $temp = $client->getTemp('New York'); // This should be a zip code
    print $temp;
} catch (SOAPFault $exception) {
    print $exception;
}
?>
SOAPFault exception: [SOAP-ENV:Server] Zip Code New York is unknown.

  in /www/www.example.com/soap.php:8
Stack trace:
#0 /www/www.example.com/soap.php(8): SOAPClient->getTemp('getTemp', Array)
#1 {main}

Since the server requires a zip code but Figure passed New York, the server returned a SOAP fault. Printing the exception gives you, among other debugging information, the error Zip Code New York is unknown.

If you dislike exceptions, make SOAP handle faults via a return code by setting the exceptions configuration setting to 0. This is done in Figure.

Detecting SOAP faults without exceptions

<?php
$wsdl_url =
    'http://www.example.com/TemperatureService.wsdl';

// Disable exceptions
$opts = array('exceptions' => 0);
$client = new SOAPClient($wsdl_url, $opts);

$temp = $client->&gt;getTemp('New York'); // This should be a Zip Code
if (is_soap_fault($temp)) {
    print $exception;
} else {
    print $temp;
}
?>
SOAPFault exception: [SOAP-ENV:Server] Zip Code New York is unknown.
  in /www/www.example.com/soap.php:8
#0 {main}

To alter the default settings for a SOAPClient object, pass in an array as the second argument to the constructor. This is the same array that you use to specify information about non-WSDL servers.

When exceptions are disabled, $temp contains either the valid response or a SOAP fault. Check is_soap_fault( ) to discover if there's an error.

See Also

Recipe 15.5 for throwing SOAP faults from a SOAP server.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows