问题描述
无论如何,SoapClient请求是否存在超时并引发异常.到目前为止,我得到了PHP Server响应超时,以我为例,为60秒.基本上,我想要的是,如果在一定时间内没有来自Web服务的任何答复,则将引发异常,并且我可以捕获它. 60秒警告不是我想要的.
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.
推荐答案
看看
如果您感到舒适并且您的环境允许您扩展课程.
if you are comfortable and your environment allows you to extend classes.
它基本上扩展了SoapClient
类,用可以处理超时的curl替换了HTTP传输:
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);
}
}
}
这篇关于PHP SoapClient超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!