Recipe 14.10. Catching SOAP Faults


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

14.10.2. 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; } ?>

14.10.3. 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 Example 14-2.

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 Example 14-2 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 Example 14-3.

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.

14.10.4. See Also

Recipe 15.5 for throwing SOAP faults from a SOAP server.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net