From 6ee64527487c0d61370cae72483c5a8bc91bf125 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Mon, 15 Jun 2015 17:50:31 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=BA=D1=83=D1=80=D1=81=D0=B8?= =?UTF-8?q?=D0=B2=D0=BD=D0=B0=D1=8F=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5=20timeout,=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=BE=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BE=D0=BA=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/RetailCrm/Http/Client.php | 31 ++++++++++++++++++----- tests/RetailCrm/Tests/Http/ClientTest.php | 6 ++--- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index a3374dc..c0814c4 100644 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -15,7 +15,7 @@ class Client protected $url; protected $defaultParameters; - protected $retry; + public $retry; public function __construct($url, array $defaultParameters = array()) { @@ -58,22 +58,27 @@ class Client $parameters = array_merge($this->defaultParameters, $parameters); - $path = $this->url . $path; + $url = $this->url . $path; if (self::METHOD_GET === $method && sizeof($parameters)) { - $path .= '?' . http_build_query($parameters); + $url .= '?' . http_build_query($parameters); } $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $path); - curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); + curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verify); + if (!$debug) { + curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout); + } else { + curl_setopt($ch, CURLOPT_TIMEOUT_MS, (int) $timeout + ($this->retry * 2000)); + } + if (self::METHOD_POST === $method) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); @@ -86,6 +91,20 @@ class Client curl_close($ch); + if ($errno && in_array($errno, array(6, 7, 28, 34, 35)) && $this->retry < 3) { + $errno = null; + $error = null; + $this->retry += 1; + $this->makeRequest( + $path, + $method, + $parameters, + $timeout, + $verify, + $debug + ); + } + if ($errno) { throw new CurlException($error, $errno); } diff --git a/tests/RetailCrm/Tests/Http/ClientTest.php b/tests/RetailCrm/Tests/Http/ClientTest.php index 84eecf2..61e614c 100644 --- a/tests/RetailCrm/Tests/Http/ClientTest.php +++ b/tests/RetailCrm/Tests/Http/ClientTest.php @@ -61,11 +61,11 @@ class ClientTest extends TestCase /** * @group integration - * @expectedException \RetailCrm\Exception\CurlException */ - public function testMakeRequestTimeout() + public function testMakeRequestRepeatOnTimeout() { $client = static::getClient(); - $client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true); + $response = $client->makeRequest('/orders', Client::METHOD_GET, array(), 1, false, true); + $this->assertGreaterThanOrEqual(1, $client->retry); } }