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

Commit

Permalink
Add runtime exception for the rare case, if NULL in response & option…
Browse files Browse the repository at this point in the history
…al parameter Timeout in composer
  • Loading branch information
ekta-slit committed Feb 27, 2017
1 parent 4981d05 commit 5962194
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/Sendinblue/Mailin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ class Mailin
{
public $api_key;
public $base_url;
public $timeout;
public $curl_opts = array();
public function __construct($base_url,$api_key)
public function __construct($base_url,$api_key,$timeout='')
{
if(!function_exists('curl_init'))
{
throw new Exception('Mailin requires CURL module');
}
$this->base_url = $base_url;
$this->api_key = $api_key;
$this->timeout = $timeout;
}
/**
* Do CURL request with authorization
Expand All @@ -28,6 +30,10 @@ private function do_request($resource,$method,$input)
$ch = curl_init($called_url);
$auth_header = 'api-key:'.$this->api_key;
$content_header = "Content-Type:application/json";
$timeout = ($this->timeout!='')?($this->timeout):30000; //default timeout: 30 secs
if ($timeout!='' && ($timeout <= 0 || $timeout > 60000)) {
throw new Exception('value not allowed for timeout');
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// Windows only over-ride
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Expand All @@ -36,12 +42,16 @@ private function do_request($resource,$method,$input)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
$data = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch). '\n';
throw new RuntimeException('cURL error: ' . curl_error($ch));
}
if(!is_string($data) || !strlen($data)) {
throw new RuntimeException('Request Failed');
}
curl_close($ch);
return json_decode($data,true);
Expand Down
6 changes: 4 additions & 2 deletions src/Sendinblue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This is the SendinBlue Php library. It implements the various exposed APIs that

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

* Our library supports a timeout value, default is 30,000 MS ( 30 secs ), which you can pass as 3rd parameter in Mailin class Object.

* 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:

```{
Expand Down Expand Up @@ -35,7 +37,7 @@ use Sendinblue\Mailin
* This will initiate the API with the endpoint and your access key.
*
*/
$mailin = new Mailin('https://api.sendinblue.com/v2.0','Your access key');
$mailin = new Mailin('https://api.sendinblue.com/v2.0','Your access key', 5000); // Optional parameter: Timeout in MS

/** Prepare variables for easy use **/

Expand Down Expand Up @@ -145,7 +147,7 @@ List of API calls that you can make, you can click to read more about it. Please

####Recommendation:

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.
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.
```PHP
curl_setopt($ch, CURLOPT_CAINFO, "PATH_TO/cacert.pem");
```
Expand Down

0 comments on commit 5962194

Please sign in to comment.