简体   繁体   中英

PHP SOAP - Expanding __doRequest

I am trying to expand PHP's \\SoapClient to include timeout and retry capabilities...I have tried to implement both of the following two scripts without any success

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

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

The strange thing is, a completely default soap request works perfectly for me. The following script also works perfectly for me...

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

}

However when I start trying to implement my own __doRequest code I can't get it working no matter the hundred variations I have tried. Below is one of the snippets I have tried, does anyone know the default options/lines I need to use to make it behave natively?

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

Thanks for any help you can provide.

I would avoid to re-invent the wheel. The SoapClient already handles the timeout (see the connection_timeout option of the constructor in the doc .

Then you can just implement the retries doing something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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