繁体   English   中英

PHP SOAP-扩展__doRequest

[英]PHP SOAP - Expanding __doRequest

我正在尝试扩展PHP的\\ SoapClient以包括超时和重试功能...我试图实现以下两个脚本都没有成功

https://github.com/ideaconnect/idct-soap-client/blob/master/src/client.php

https://gist.github.com/RobThree/2490351

奇怪的是,完全默认的soap请求对我来说非常合适。 以下脚本也非常适合我...

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);

感谢您的任何帮助,您可以提供。

我会避免重新发明轮子。 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM