PHP SoapClient Timeout

后端 未结 6 1371
误落风尘
误落风尘 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.

提交回复
热议问题