Skip to content

Commit

Permalink
#18 Возможность указать timeout для соединения
Browse files Browse the repository at this point in the history
  • Loading branch information
ezoterik committed Mar 18, 2021
1 parent 0460cf3 commit 332dcbc
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/Delivery/NovaPoshtaApi2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
)));
}

Expand Down

0 comments on commit 332dcbc

Please sign in to comment.