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

Commit 5962194

Browse files
committed
Add runtime exception for the rare case, if NULL in response & optional parameter Timeout in composer
1 parent 4981d05 commit 5962194

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Sendinblue/Mailin.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ class Mailin
99
{
1010
public $api_key;
1111
public $base_url;
12+
public $timeout;
1213
public $curl_opts = array();
13-
public function __construct($base_url,$api_key)
14+
public function __construct($base_url,$api_key,$timeout='')
1415
{
1516
if(!function_exists('curl_init'))
1617
{
1718
throw new Exception('Mailin requires CURL module');
1819
}
1920
$this->base_url = $base_url;
2021
$this->api_key = $api_key;
22+
$this->timeout = $timeout;
2123
}
2224
/**
2325
* Do CURL request with authorization
@@ -28,6 +30,10 @@ private function do_request($resource,$method,$input)
2830
$ch = curl_init($called_url);
2931
$auth_header = 'api-key:'.$this->api_key;
3032
$content_header = "Content-Type:application/json";
33+
$timeout = ($this->timeout!='')?($this->timeout):30000; //default timeout: 30 secs
34+
if ($timeout!='' && ($timeout <= 0 || $timeout > 60000)) {
35+
throw new Exception('value not allowed for timeout');
36+
}
3137
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
3238
// Windows only over-ride
3339
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
@@ -36,12 +42,16 @@ private function do_request($resource,$method,$input)
3642
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
3743
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
3844
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
45+
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
3946
curl_setopt($ch, CURLOPT_HEADER, 0);
4047
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
4148
$data = curl_exec($ch);
4249
if(curl_errno($ch))
4350
{
44-
echo 'Curl error: ' . curl_error($ch). '\n';
51+
throw new RuntimeException('cURL error: ' . curl_error($ch));
52+
}
53+
if(!is_string($data) || !strlen($data)) {
54+
throw new RuntimeException('Request Failed');
4555
}
4656
curl_close($ch);
4757
return json_decode($data,true);

src/Sendinblue/README.md

Lines changed: 4 additions & 2 deletions
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
* You can install the SendinBlue API using [Composer](https://packagist.org/packages/mailin-api/mailin-api-php). Just add the following to your composer.json:
1113

1214
```{
@@ -35,7 +37,7 @@ use Sendinblue\Mailin
3537
* This will initiate the API with the endpoint and your access key.
3638
*
3739
*/
38-
$mailin = new Mailin('https://api.sendinblue.com/v2.0','Your access key');
40+
$mailin = new Mailin('https://api.sendinblue.com/v2.0','Your access key', 5000); // Optional parameter: Timeout in MS
3941
4042
/** Prepare variables for easy use **/
4143
@@ -145,7 +147,7 @@ List of API calls that you can make, you can click to read more about it. Please
145147

146148
####Recommendation:
147149

148-
If you face any error like "Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:func(144):reason(134)\n", with our library then by adding the below line of code just before curl_exec() ( line no. 37 ) in mailin.php file, you may no longer face this issue.
150+
If you face any error like "Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:func(144):reason(134)\n", with our library then by adding the below line of code just before curl_exec() ( line no. 48 ) in mailin.php file, you may no longer face this issue.
149151
```PHP
150152
curl_setopt($ch, CURLOPT_CAINFO, "PATH_TO/cacert.pem");
151153
```

0 commit comments

Comments
 (0)