PHP SoapClient Timeout

后端 未结 6 1362
误落风尘
误落风尘 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: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;
        }
    
    }
    

提交回复
热议问题