Skip to content
This repository was archived by the owner on May 18, 2020. It is now read-only.

Commit 519df86

Browse files
committed
API-38: Added default timeout of 30 secs
1 parent 10ffba0 commit 519df86

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

V2.0/Mailin.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ class Mailin
88
{
99
public $api_key;
1010
public $base_url;
11+
public $timeout;
1112
public $curl_opts = array();
12-
public function __construct($base_url,$api_key)
13+
public function __construct($base_url,$api_key,$timeout='')
1314
{
1415
if(!function_exists('curl_init'))
1516
{
16-
throw new Exception('Mailin requires CURL module');
17+
throw new RuntimeException('Mailin requires cURL module');
1718
}
1819
$this->base_url = $base_url;
1920
$this->api_key = $api_key;
21+
$this->timeout = $timeout;
2022
}
2123
/**
2224
* Do CURL request with authorization
@@ -27,6 +29,10 @@ private function do_request($resource,$method,$input)
2729
$ch = curl_init($called_url);
2830
$auth_header = 'api-key:'.$this->api_key;
2931
$content_header = "Content-Type:application/json";
32+
$timeout = ($this->timeout!='')?($this->timeout):30000; //default timeout: 30 secs
33+
if ($timeout!='' && ($timeout <= 0 || $timeout > 60000)) {
34+
throw new Exception('value not allowed for timeout');
35+
}
3036
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
3137
// Windows only over-ride
3238
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
@@ -35,12 +41,13 @@ private function do_request($resource,$method,$input)
3541
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
3642
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
3743
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
44+
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
3845
curl_setopt($ch, CURLOPT_HEADER, 0);
3946
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
4047
$data = curl_exec($ch);
4148
if(curl_errno($ch))
4249
{
43-
echo 'Curl error: ' . curl_error($ch). '\n';
50+
throw new RuntimeException('cURL error: ' . curl_error($ch));
4451
}
4552
curl_close($ch);
4653
return json_decode($data,true);

V2.0/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This is the SendinBlue Php library. It implements the various exposed APIs that
77

88
* You will need to first get the Access key from [SendinBlue](https://www.sendinblue.com).
99

10+
* Our library supports a timeout value, default is 30,000 MS ( 30 secs ), which you can pass as 3rd parameter in Mailin class Object.
11+
1012
* Assuming that you have cloned this git repo, or downloaded Mailin.php and its in the same directory than the script. You can use this small sample script to get started.
1113

1214
```PHP
@@ -16,7 +18,7 @@ require('Mailin.php');
1618
* This will initiate the API with the endpoint and your access key.
1719
*
1820
*/
19-
$mailin = new Mailin('https://api.sendinblue.com/v2.0','Your access key');
21+
$mailin = new Mailin('https://api.sendinblue.com/v2.0','Your access key', 5000); // Optional parameter: Timeout in MS
2022

2123
/** Prepare variables for easy use **/
2224

0 commit comments

Comments
 (0)