Skip to content

Commit

Permalink
Merge pull request #54 from ezoterik/set-timeout
Browse files Browse the repository at this point in the history
#18 Возможность указать timeout для соединения
  • Loading branch information
lis-dev authored Mar 19, 2021
2 parents 0460cf3 + 332dcbc commit 806457c
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 806457c

Please sign in to comment.