我正在尝试扩展PHP的\ SoapClient以包含超时和重试功能...我试图实现以下两个脚本而没有任何成功
https://github.com/ideaconnect/idct-soap-client/blob/master/src/client.php
https://gist.github.com/RobThree/2490351
奇怪的是,完全默认的肥皂请求对我来说非常合适。以下脚本也适用于我......
class SoapTest extends \SoapClient {
public function __construct($wsdl, $options) {
parent::__construct($wsdl,$options);
}
public function __doRequest ($request, $location, $action, $version, $one_way = null) {
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
return $response;
}
}
然而,当我开始尝试实现自己的__doRequest代码时,无论我尝试了多少百种变化,我都无法使其工作。下面是我尝试过的一个片段,是否有人知道我需要使用的默认选项/行来使其表现为原生?
$curl = curl_init($location);
$options = array(
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request,
CURLOPT_HEADER => false,
CURLOPT_NOSIGNAL => false,
CURLOPT_HTTPHEADER => array(sprintf('Content-Type: %s', $version == 2 ? 'application/soap+xml' : 'text/xml'), sprintf('SOAPAction: %s', $action)),
CURLOPT_SSL_VERIFYPEER => $this->sslverifypeer
);
if ($this->timeout>0) {
if (defined('CURLOPT_TIMEOUT_MS')) { //Timeout in MS supported?
$options[CURLOPT_TIMEOUT_MS] = $this->timeout;
} else { //Round(up) to second precision
$options[CURLOPT_TIMEOUT] = ceil($this->timeout/1000);
}
}
if ($this->connecttimeout>0) {
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) { //ConnectTimeout in MS supported?
$options[CURLOPT_CONNECTTIMEOUT_MS] = $this->connecttimeout;
} else { //Round(up) to second precision
$options[CURLOPT_CONNECTTIMEOUT] = ceil($this->connecttimeout/1000);
}
}
if (curl_setopt_array($curl, $options) === false)
throw new Exception('Failed setting CURL options');
$response = curl_exec($curl);
感谢您提供的任何帮助。
答案 0 :(得分:0)
我会避免重新发明轮子。 SoapClient已经处理了超时(请参阅doc中构造函数的 connection_timeout 选项。
然后你可以实现类似
的重试public function __doRequest ($request, $location, $action, $version, $one_way = null) {
try {
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
} catch (SoapFault $e) {
// handle the retry
}
return $response;
}