簡體   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