PHP SoapClient Timeout PHP SoapClient Timeout php php

PHP SoapClient Timeout


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 the stream_context option for new SoapClient()Docs. Under the surface SoapClient uses the HTTP and SSL transports.


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.


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);        }    }}