无论如何,SoapClient请求超时并抛出异常。截至目前,我得到PHP服务器响应超时,在我的情况下60秒。基本上我想要的是,如果在一定时间内没有来自Web服务的任何回复,将抛出异常并且我可以捕获它。 60秒警告不是我想要的。
答案 0 :(得分:48)
虽然Andrei与一个不错的解决方案相关联,但是这个解决方案的代码较少,但却找到了一个很好的解决方案:
示例代码:
//
// setting a connection timeout (fifteen seconds on the example)
//
$client = new SoapClient($wsdl, array("connection_timeout" => 15));
如果您需要更细粒度的HTTP控件,还有流上下文。请参阅new SoapClient()
Docs的stream_context
选项。表面SoapClient
下使用HTTP和SSL传输。
答案 1 :(得分:33)
ini_set("default_socket_timeout", 15);
$client = new SoapClient($wsdl, array(......));
connection_timeout选项定义了以秒为单位的超时 与SOAP服务的连接。此选项未定义超时 对于响应缓慢的服务。限制等待通话的时间 完成default_socket_timeout设置是可用的。
答案 2 :(得分:17)
看看
如果您感觉舒适并且您的环境允许您扩展课程。
它基本上扩展了SoapClient
类,用curl替换了HTTP传输,可以处理超时:
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);
}
}
}
答案 3 :(得分:4)
接受的答案将破坏SoapClient提供的所有功能。就像设置正确的内容标题,身份验证等一样。
这将是解决问题的更好方法
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;
}
}
答案 4 :(得分:1)
您可以通过编辑器安装: https://github.com/ideaconnect/idct-soap-client
它扩展了标准的SoapClient,并提供了设置重试次数,连接次数和读取超时的选项。
答案 5 :(得分:0)
在使用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;
}
这可以帮助我最多等待10秒来建立连接,而等待60秒来执行呼叫。