Is there anyway for a SoapClient Request to time out and throw an exception. As of now, I get PHP Server response timeout, in my case 60 seconds. Basically what I want is, if there isn't any reply from the Web Service within certain time, an exception would be thrown and I could catch it. The 60 seconds warning is not what I want.
7 Answers
ini_set("default_socket_timeout", 15);
$client = new SoapClient($wsdl, array(......));
The connection_timeout option defines a timeout in seconds for the connection to the SOAP service. This option does not define a timeout for services with slow responses. To limit the time to wait for calls to finish the default_socket_timeout setting is available.

- 669
- 1
- 5
- 2
-
This is the answer. :) – Alain Tiemblo Sep 20 '13 at 08:34
-
This is a nice answer as long as the socket timeout is not exceeded. Otherwise the server will throw a Connection Timeout and execution will stop... – Nico Mar 31 '14 at 10:12
-
1Also note, that there's still this unsolved PHP-Bug: https://bugs.php.net/bug.php?id=41631. So `default_socket_timeout` may not work with HTTPS connections – Michael Härtl Jun 18 '14 at 10:44
-
`default_socket_timeout` works for service with slow response. The question was about handling a service that's _not_ responding. – T30 May 10 '17 at 09:13
-
@T30 - it's the same thing. In either case this setting will kill the request after X seconds. – But those new buttons though.. Jul 13 '21 at 13:07
Invalid answer. Please see https://stackoverflow.com/a/12119215/441739 instead.
While Andrei linked to a decent solution, this one has less code yet arrives at a good solution:
* Handling Timeouts with PHP5 SoapClient Extension (by Antonio Ramirez; 02 Feb 2010)
Example code:
//
// setting a connection timeout (fifteen seconds on the example)
//
$client = new SoapClient($wsdl, array("connection_timeout" => 15));
And there is also the stream context, if you need more fine-grained HTTP control. See thestream_context
option fornew SoapClient()
Docs. Under the surfaceSoapClient
uses the HTTP and SSL transports.

- 2,292
- 2
- 19
- 31
-
1
-
@DanFromGermany, what version of PHP are you running? Are you passing any options other than what's shown above? – Jon L. May 21 '14 at 11:58
-
1PHP 5.3, I'm also passing `'soap_version' => SOAP_1_1,` but I'm using `Zend_Soap_Client`, but it just extends `SoapClient` – Daniel W. May 21 '14 at 12:03
-
1@DanFromGermany, actually Zend_Soap_Client wraps SoapClient, rather than extend it. You can see the list of options that Zend_Soap_Client supports here: https://github.com/zendframework/zf2/blob/master/library/Zend/Soap/Client.php#L166 << You'll notice that `connection_timeout` isn't a supported option. – Jon L. May 22 '14 at 12:33
-
-
6Per php docs this timeout only affects the connection to the service, not the time the operation actually takes. You need to set the `default_socket_timeout` like mentioned below – staabm Jul 22 '17 at 10:49
-
Have a look at
if you are comfortable and your environment allows you to extend classes.
It basically extends the SoapClient
class, replaces the HTTP transport with curl which can handle the timeouts:
class SoapClientTimeout extends SoapClient
{
private $timeout;
public function __setTimeout($timeout)
{
if (!is_int($timeout) && !is_null($timeout))
{
throw new Exception("Invalid timeout value");
}
$this->timeout = $timeout;
}
public function __doRequest($request, $location, $action, $version, $one_way = FALSE)
{
if (!$this->timeout)
{
// Call via parent because we require no timeout
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
}
else
{
// Call via Curl and use the timeout
$curl = curl_init($location);
curl_setopt($curl, CURLOPT_VERBOSE, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
$response = curl_exec($curl);
if (curl_errno($curl))
{
throw new Exception(curl_error($curl));
}
curl_close($curl);
}
// Return?
if (!$one_way)
{
return ($response);
}
}
}

- 193,403
- 52
- 435
- 836

- 5,828
- 5
- 39
- 66
-
Thanks. That would be one place to have a look. Lets see if there is any other. – Shamim Hafiz - MSFT Aug 17 '10 at 08:44
-
2I've tried this in PHP 5.4.6, but it seems, they have changed the signature of `_doRequest()` without further notice. `$request` does not contain the raw XML anymore, but rather a string like `141201299690460051141201717499383133141201717499423132141201717499443131141201717499463131141201717499483135false`. Can anyone shed some light on this? – Michael Härtl Oct 06 '14 at 10:51
-
1
-
@MichaelHärtl can you provide some more info? This could be a big problem, but i'm having a difficult time reproducing this. – Nanne Sep 22 '16 at 09:03
-
@Nanne Unfortunately not. I'm not sure what caused the issues I had and what I changed to fix it. But from looking at my code, I've ended up using a similar approach to the solution described here and it worked. But maybe I've simply upgraded to a later PHP version than 5.4.6. Don't remember. – Michael Härtl Sep 22 '16 at 12:59
-
-
Din't work entirely. I had to also add header `'SOAPAction: ' . $action.` – Charalampos Tsimpouris Jun 05 '18 at 10:38
The accepted answer will break all functionalities that SoapClient has to offer. Like setting the correct content headers, authentication etc.
This would be a better solution to the problem
class MySoapClient extends \SoapClient
{
private $timeout = 10;
public function __construct($wsdl, array $options)
{
// Defines a timeout in seconds for the connection to the SOAP service.
// This option does not define a timeout for services with slow responses.
// To limit the time to wait for calls to finish the default_socket_timeout setting is available.
if (!isset($options['connection_timeout'])) {
$options['connection_timeout'] = $this->timeout;
}
parent::__construct($wsdl, $options);
}
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
$original = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', $this->timeout);
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
ini_set('default_socket_timeout', $original);
return $response;
}
}

- 4,144
- 1
- 31
- 42
-
1For an extra safety net you should try/catch the parent call (e.g. Some apps/frameworks could throw Exceptions on Warnings via a custom error_handler) which would otherwise leave the un-resetted `default_socket_timeout` – staabm Jul 22 '17 at 11:00
-
The connection_timeout itself should default to a value a lot smaller then 10secs. – staabm Jul 22 '17 at 11:02
You could also use stream_context_create() and add the timeout
option to the http
array:
$context = stream_context_create(
array(
'http' => array(
"timeout" => 10,
),
)
);
The SoapHandler initialization then should be:
$soapHandler = new SoapClient($wsdl, [
//more params, if needed..
'stream_context' => $context,
]);

- 3,061
- 4
- 37
- 44
-
2
-
-
I would compare the elapsed time to the timeout in case of an exception. If they're close, you got a timeout. – Jorrit Schippers Sep 19 '22 at 19:43
You can install this through composer: https://github.com/ideaconnect/idct-soap-client
It extends the standard SoapClient and gives options to set the amount of retries, connection and read timeouts.

- 2,860
- 18
- 31

- 1,278
- 1
- 8
- 17
I am using the following logic when working with SOAPClient:
public function executeSoapCall($method, $params)
{
try {
$client = $this->tryGetSoapClient();
$timeout = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', 60);//set new timeout value - 60 seconds
$client->__soapCall($method, $params);//execute SOAP call
ini_set('default_socket_timeout', $timeout);//revert timeout back
} catch (\Throwable $e) {
if (isset($timeout)) {
ini_set('default_socket_timeout', $timeout);//revert timeout back
}
}
}
protected function tryGetSoapClient()
{
$timeout = ini_get('default_socket_timeout');//get timeout (need to be reverted back afterwards)
ini_set('default_socket_timeout', 10);//set new timeout value - 10 seconds
try {
$client = new \SoapClient($this->wsdl, $this->options);//get SOAP client
} catch (\Throwable $e) {
ini_set('default_socket_timeout', 10);//revert back in case of exception
throw $e;
}
$this->iniSetTimeout($timeout);//revert back
return $client;
}
This helps me to wait up to 10 seconds for connection establishment, and 60 seconds for the call execution.

- 21
- 2