-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTextClient.php
More file actions
135 lines (112 loc) · 3.26 KB
/
TextClient.php
File metadata and controls
135 lines (112 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace CMText;
use CMText\Exceptions\MessagesLimitException;
use CMText\Exceptions\RecipientLimitException;
/**
* Class TextClient
*
* @package CMText
*/
class TextClient implements ITextClient
{
/**
* @var string
*/
private $gateway;
/**
* @var
*/
private $apiKey;
/**
* Maximum amount of Message objects allowed per request
*/
const MESSAGES_MAXIMUM = 1000;
/**
* SDK Version constant
*/
const VERSION = '3.0.2';
/**
* TextClient constructor.
*
* @param string $apiKey
* @param string $gateway optional
*/
public function __construct(
string $apiKey,
$gateway = Gateways::GLOBAL
)
{
// load the CM API KEY for authentication against the gateway
$this->apiKey = $apiKey;
// set the Gateway to use
$this->gateway = $gateway;
}
/**
* Fast and easy method to instantly send one message.
*
* @param string $message - Message body to send
* @param string $from - Sender name
* @param array $to - Recipient phonenumbers
* @param string|null $reference optional
*
* @return TextClientResult
* @throws RecipientLimitException
* @throws MessagesLimitException
*/
public function SendMessage(
string $message,
string $from,
array $to,
?string $reference = null
)
{
// send it out instantly
return self::send([
new Message($message, $from, $to, $reference)
]);
}
/**
* Send an array of Message objects.
*
* @param array $messages Array of Message objects
*
* @return TextClientResult
* @throws MessagesLimitException
*/
public function send(
array $messages
)
{
if(count($messages) > self::MESSAGES_MAXIMUM){
throw new MessagesLimitException('Maximum amount of Message objects exceeded. ('. self::MESSAGES_MAXIMUM .')');
}
$requestModel = new TextClientRequest($this->apiKey, $messages);
$ch = curl_init($this->gateway);
try {
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($requestModel),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen( json_encode($requestModel) ),
'X-CM-SDK: ' . 'text-sdk-php-' . self::VERSION,
],
CURLOPT_TIMEOUT => 20,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FORBID_REUSE => true,
]);
$response = curl_exec($ch);
$statuscode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
// curl errors will raise an exception
if( curl_error($ch) ){
throw new \Exception( curl_error($ch) );
}
}catch (\Exception $exception){
$response = json_encode(['details' => $exception->getMessage()]);
$statuscode = TextClientStatusCodes::UNKNOWN;
}
$return = new TextClientResult($statuscode, $response);
return $return;
}
}