PHP SoapClient Timeout

后端 未结 6 1357
误落风尘
误落风尘 2020-12-05 00:12

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, i

相关标签:
6条回答
  • 2020-12-05 00:32

    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.

    0 讨论(0)
  • 2020-12-05 00:40

    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.

    0 讨论(0)
  • 2020-12-05 00:42

    Have a look at

    • Timing Out PHP Soap Calls by Robert; 21 Oct 2009

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 00:42

    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.

    0 讨论(0)
  • 2020-12-05 00:45

    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;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 00:50
    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.

    0 讨论(0)
提交回复
热议问题