views:

3199

answers:

3

I'm working on a project where I am verifying information from a user with a SOAP web service. I currently am taking care of errors assuming that I'm receiving responses from the web service, but also need to handle the edge cases of a service timeout or unavailability.

In the case of a timeout or service unavailability, I need to pretend that the request was successful (that the web service approved the info), but I'm not clear on what exceptions are thrown.

Some pseudo-code:

// $client is PHP's SoapClient class
try {
  $response = $client->SomeSoapRequest();
}
catch(SoapFault $e){
  // handle issues returned by the web service
}
catch(Exception $e){
  // handle PHP issues with the request
}

What I can't seem to find is:

  1. Are timeouts a SoapFault? If so, what is the best way to distinguish between a timeout error and web service issues (like a type error, etc.)? I found one page that mentioned an error where the message was something to the effect of "Error loading headers", but didn't mention if this was a Soap fault.
  2. How is a service unavailability potentially going to happen? A PHP exception seems like it would make sense (a SoapFault would be returned from the web service where unavailability would be a socket issue or similar)?
  3. Is there an existing service (e.g. example) that I can test a timeout against? Most timeout related discussions seem to be related to preventing timeouts by extending the default timeout setting, which isn't ideal in this situation.

Thanks!

+1  A: 

To deal with timeouts in the service

$client = new SoapClient($wsdl, array("connection_timeout"=>10));

// SET SOCKET TIMEOUT if(defined('RESPONSE_TIMEOUT') && RESPONSE_TIMEOUT != '') { ini_set('default_socket_timeout', RESPONSE_TIMEOUT); }

ToughPal
This just helps make sure a slow-responding service does not time out. I'm more interested in letting the service time out and catching that error (versus other service-specific errors). Basically the web service could have maintenance windows or high traffic making it slow or unresponsive, but my end users don't need to pay the price for that.
Rob
+1  A: 

The best way to fix this, if you're comfortable with using CURL, is to extend the SoapClient class and override the __doRequest() function. I've got a post on my blog where I explain what I did to resolve this issue:

http://www.darqbyte.com/2009/10/21/timing-out-php-soap-calls/

Robert F. Ludwick
A: 

Looks like default_socket_timeout is not taken into account when making SOAP calls over HTTPS: http://bugs.php.net/bug.php?id=48524. Bug open at the time of writing. As a comment on the blog post Robert Ludwick references above points out, the workaround the post discusses works around this bug also.

Toby Champion