Skip to content

Commit 38da58f

Browse files
committed
Merge pull request #15 from messagebird/ca-cert-usage
Use ca-bundle when doing a cURL request to the REST API
2 parents 9e9cc56 + c3e60db commit 38da58f

File tree

6 files changed

+3996
-7
lines changed

6 files changed

+3996
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "messagebird/php-rest-api",
33
"description": "MessageBird REST API client for PHP",
4-
"version": "1.3.0",
4+
"version": "1.3.1",
55
"type": "library",
66
"homepage": "https://github.com/messagebird/php-rest-api",
77
"license": "BSD-2-Clause",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../autoload.php');
4+
5+
$MessageBird = new \MessageBird\Client('YOUR_ACCESS_KEY'); // Set your own API access key here.
6+
7+
$Message = new \MessageBird\Objects\Message();
8+
$Message->originator = 'MessageBird';
9+
$Message->recipients = array(31612345678);
10+
$Message->body = 'This is a test message with a smiling emoji 😀.';
11+
$Message->datacoding = 'unicode';
12+
13+
try {
14+
$MessageResult = $MessageBird->messages->create($Message);
15+
var_dump($MessageResult);
16+
17+
} catch (\MessageBird\Exceptions\AuthenticateException $e) {
18+
// That means that your accessKey is unknown
19+
echo 'wrong login';
20+
21+
} catch (\MessageBird\Exceptions\BalanceException $e) {
22+
// That means that you are out of credits, so do something about it.
23+
echo 'no balance';
24+
25+
} catch (\Exception $e) {
26+
echo $e->getMessage();
27+
}

examples/message-create.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$Message = new \MessageBird\Objects\Message();
88
$Message->originator = 'MessageBird';
99
$Message->recipients = array(31612345678);
10-
$Message->body = 'This is a test message.';
10+
$Message->body = 'This is a test message.';
1111

1212
try {
1313
$MessageResult = $MessageBird->messages->create($Message);

src/MessageBird/Client.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Client
1212

1313
const ENDPOINT = 'https://rest.messagebird.com';
1414

15-
const CLIENT_VERSION = '1.3.0';
15+
const CLIENT_VERSION = '1.3.1';
1616

1717
/**
1818
* @var string
@@ -59,9 +59,9 @@ class Client
5959
*/
6060
protected $HttpClient;
6161

62-
6362
/**
64-
* @param $accessKey
63+
* @param string $accessKey
64+
* @param Common\HttpClient $httpClient
6565
*/
6666
public function __construct($accessKey = null, Common\HttpClient $httpClient = null)
6767
{
@@ -95,6 +95,9 @@ public function setAccessKey ($accessKey)
9595
$this->HttpClient->setAuthentication($Authentication);
9696
}
9797

98+
/**
99+
* @return string
100+
*/
98101
private function getPhpVersion()
99102
{
100103
if (!defined('PHP_VERSION_ID')) {

src/MessageBird/Common/HttpClient.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class HttpClient
1919
const HTTP_NO_CONTENT = 204;
2020

2121
/**
22-
* @var
22+
* @var string
2323
*/
2424
protected $endpoint;
2525

@@ -34,7 +34,7 @@ class HttpClient
3434
protected $Authentication;
3535

3636
/**
37-
* @param $endpoint
37+
* @param string $endpoint
3838
*/
3939
public function __construct($endpoint)
4040
{
@@ -112,6 +112,13 @@ public function performHttpRequest($method, $resourceName, $query = null, $body
112112
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::REQUEST_DELETE);
113113
}
114114

115+
// Some servers have outdated or incorrect certificates, Use the included CA-bundle
116+
$caFile = realpath(dirname(__FILE__) . "/../ca-bundle.crt");
117+
if (!file_exists($caFile)) {
118+
throw new Exceptions\HttpException('Unable to find CA-bundle file: ' . $caFile);
119+
}
120+
curl_setopt($curl, CURLOPT_CAINFO, $caFile);
121+
115122
$response = curl_exec($curl);
116123

117124
if ($response === false) {

0 commit comments

Comments
 (0)