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