とにかく、SoapClientリクエストがタイムアウトして例外をスローすることはありますか?今のところ、PHPサーバーの応答タイムアウト(私の場合は60秒)が発生します。基本的に私が欲しいのは、一定時間内にWebサービスからの応答がない場合、例外がスローされ、それをキャッチできることです。60秒の警告は私が望むものではありません。
7 に答える
無効な回答です。代わりにhttps://stackoverflow.com/a/12119215/441739を参照してください。
Andrei はまともな解決策にリンクしていますが、これはコードが少なくても良い解決 策 にたどり着き
ます :例) // $client = new SoapClient($wsdl, array("connection_timeout" => 15)); また、よりきめ細かい HTTP コントロールが必要な場合は、ストリーム コンテキストもあります。Docsのオプションを参照してください。水面下では、HTTP および SSL トランスポートを使用します。stream_context
new SoapClient()
SoapClient
ini_set("default_socket_timeout", 15);
$client = new SoapClient($wsdl, array(......));
connection_timeout オプションは、SOAP サービスへの接続のタイムアウトを秒単位で定義します。このオプションは、応答が遅いサービスのタイムアウトを定義しません。呼び出しが終了するまでの待機時間を制限するには、default_socket_timeout 設定を使用できます。
見て
あなたが快適で、あなたの環境がクラスを拡張できる場合。
基本的にSoapClient
クラスを拡張し、HTTP トランスポートをタイムアウトを処理できる curl に置き換えます。
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);
}
}
}
受け入れられた答えは、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;
}
}
これは composer からインストールできます: https://github.com/ideaconnect/idct-soap-client
標準の SoapClient を拡張し、再試行回数、接続、および読み取りタイムアウトを設定するオプションを提供します。
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 秒待つことができます。