Handling Timeouts with PHP5 SoapClient Extension

I was breaking my head towards a silly thing around SoapClient Calls. My actual project requires Soap Calls to certain providers and everytime I was calling the external server, I had to wait for the server response (if any) and if the server failed to respond accordingly… damn… Warnings and/or Errors displayed and boom… My great application just looked like… well, forget it, I just used the try and catch statements for that… not big deal.

So I jump over Google’s horse and looked around implementing timeouts! because that was a true issue. Can you imagine? A user makes a request I looked around my DB and suddenly that single SOAP call takes me one minute to respond. No good, time for a solution.

Found the way to do it, by setting a hidden parameter (not documented) named ‘connection_timeout’:

// setting a connection timeout (five seconds on the example)
$client = new SoapClient($wsdl, array("connection_timeout"=>15));

The above says wait 15 seconds before returning a fault if you cannot connect properly to the host. The parameter ‘connection_timeout’ addresses the time it takes to wait to connect to your host.

As Jim Plush’s said:

You’d have two timeouts set in your application one for how long it should take to actually connect to your remote host and a timeout for how long the socket connection should wait for a response from the server.

So, now we need to set the time we wait for a server response with the following:

ini_set('default_socket_timeout', 180);

With these two configuration options we control the time to wait for a Soap host connection and the time waiting for a Soap Service response. Just perfect.

One thought on “Handling Timeouts with PHP5 SoapClient Extension

Leave a Reply

Your email address will not be published. Required fields are marked *