From 332dcbc2b714aa547484282249cc3a708e1b0a09 Mon Sep 17 00:00:00 2001 From: Oleg Balykin Date: Thu, 18 Mar 2021 23:32:22 +0200 Subject: [PATCH] =?UTF-8?q?#18=20=D0=92=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D1=83=D0=BA=D0=B0=D0=B7=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20timeout=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BE?= =?UTF-8?q?=D0=B5=D0=B4=D0=B8=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Delivery/NovaPoshtaApi2.php | 45 +++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Delivery/NovaPoshtaApi2.php b/src/Delivery/NovaPoshtaApi2.php index 70c9b6f..042b17d 100644 --- a/src/Delivery/NovaPoshtaApi2.php +++ b/src/Delivery/NovaPoshtaApi2.php @@ -45,6 +45,9 @@ class NovaPoshtaApi2 */ protected $connectionType = 'curl'; + /** @var int Connection timeout (in seconds) */ + protected $timeout = 0; + /** * @var string Areas (loaded from file, because there is no so function in NovaPoshta API 2.0) */ @@ -131,6 +134,26 @@ public function getConnectionType() return $this->connectionType; } + /** + * @param int $timeout + * + * @return $this + */ + public function setTimeout($timeout) + { + $this->timeout = (int)$timeout; + + return $this; + } + + /** + * @return int + */ + public function getTimeout() + { + return $this->timeout; + } + /** * Setter for language property. * @@ -259,16 +282,28 @@ private function request($model, $method, $params = null) curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); + + if ($this->timeout > 0) { + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); + } + $result = curl_exec($ch); curl_close($ch); } } else { + $httpOptions = array( + 'method' => 'POST', + 'header' => "Content-type: application/x-www-form-urlencoded;\r\n", + 'content' => $post, + ); + + if ($this->timeout > 0) { + $httpOptions['timeout'] = $this->timeout; + } + $result = file_get_contents($url, false, stream_context_create(array( - 'http' => array( - 'method' => 'POST', - 'header' => "Content-type: application/x-www-form-urlencoded;\r\n", - 'content' => $post, - ), + 'http' => $httpOptions, ))); }