douyan1896 2010-08-17 08:13
浏览 196
已采纳

PHP SoapClient超时

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, if there isn't any reply from the Web Service within certain time, an exception would be thrown and I could catch it. The 60 seconds warning is not what I want.

  • 写回答

6条回答 默认 最新

  • douhuangjie4503 2010-08-17 08:17
    关注

    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);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)