PHP SoapClient Timeout

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

提交回复
热议问题