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:
- 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. - 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)?
- 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.
1) In case of timeout, PHP throws a SoapFault exception with
faultcode="HTTP"
andfaultstring="Error Fetching http headers"
.2) In my opinion, the best way to distinguish between a timeout error and web service issues is by looking at the
faultcode
andfaultstring
members of the SoapFault class.In particular, the
faultcode
element is intended for use by software to provide an algorithmic mechanism for identifying the fault.As you can also read in a comment of the PHP manual, there is no method to read the
faultcode
property, so you have to access it directly (eg.$e->faultcode
), because thegetCode()
method does not work.The SOAP 1.1 Spec defines four possible values for the
faultcode
field:In addition to those codes, PHP uses the
HTTP
code for identifying the errors happening at the protocol level (eg.: socket errors); for example, if you search foradd_soap_fault
in the ext/soap/php_http.c source code you can see when some of these kind of faults are generated.By searching for the
add_soap_fault
andsoap_server_fault
functions in the PHP SOAP extension source files, I've built the following list of PHPSoapFault
exceptions:3) To simulate the timeout condition, try with the following code:
soapclient.php
soapserver.php
Notice the
sleep
call in theSoapServer.php
script with a time (20) longest than the time (10) specified for thedefault_socket_timeout
parameter in theSoapClient.php
script.If you want to simulate a service unavailability, you could for example change the
location
protocol fromhttp
tohttps
in thesoapclient.php
script, assuming that your web server is not configured for SSL; by doing this, PHP should throw a "Could not connect to host" SoapFault.Guess I'm little late, but in case someone is still looking for solution to timeouts in php soap client - here's what's worked for me:
Basicly replacing PHP SoapClient with cURL with set timeout. Just keep in mind, sometimes WS expects action specified in HTTP header. Original solution posted on that website doesn't include that (check comments).