This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smsbroadcastau.class.php
369 lines (335 loc) · 10.4 KB
/
smsbroadcastau.class.php
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* @file
* Contains class to integrate with "SMS Broadcast" SMS gateway API.
*
* @see https://www.smsbroadcast.com.au
* @see https://www.smsbroadcast.com.au/Advanced%20HTTP%20API.pdf
*/
class smsbroadcastau {
/**
* API endpoint URL.
*/
protected $api_endpoint = 'https://www.smsbroadcast.com.au/api-adv.php';
/**
* API account username.
*
* Your SMS Broadcast username. This is the same username that you would use
* to login to the SMS Broadcast website.
*
* @see smsbroadcastau::setAuthentication()
*/
protected $api_username = '';
/**
* API account password.
*
* Your SMS Broadcast password. This is the same password that you would use
* to login to the SMS Broadcast website.
*
* @see smsbroadcastau::setAuthentication()
*/
protected $api_password = '';
/**
* Array of recipient phone numbers.
*
* The numbers can be in the format:
* - 04xxxxxxxx (Australian format)
* - 614xxxxxxxx (International format without a preceding +)
* - 4xxxxxxxx (missing leading 0)
* SMS Broadcast recommend using the international format, but your messages
* will be accepted in any of the above formats. The numbers should contain
* only numbers, with no spaces or other characters.
*
* @see smsbroadcastau::addRecipient()
*/
public $recipients = array();
/**
* The sender ID for the messages.
*
* Can be a mobile number or letters, up to 11 characters and should not
* contain punctuation or spaces. Leave blank to use SMS BroadcastÕs 2-way
* number.
*/
public $sender = '';
/**
* The content of the SMS message.
*
* Must not be longer than 160 characters unless the maxsplit parameter is
* used. Must be URL encoded.
*/
public $message = '';
/**
* Determines the maximum length of your SMS message.
*
* Standard SMS messages are limited to 160 characters, however our system
* allows you to send SMS messages up to 765 characters. This is achieved by
* splitting the message into parts. Each part is a normal SMS and is charged
* at the normal price. The SMS is then reconstructed by the receiving mobile
* phone and should display as a single SMS.
*
* The maxsplit setting determines how many times you are willing to split the
* message. This allows you to control the maximum cost and length of each
* message. The default setting is 1 (160 characters). The maximum is 5 (765
* characters).
*
* If your SMS is 160 characters or less, it will be sent (and cost) as a
* single SMS, regardless of the value of this setting.
*
* If your message is longer than 160 characters, it is split into parts of up
* to 153 characters (not 160).
*
* If set to NULL, the class automatically detects the value for you in the
* send() method.
*/
public $maxsplit = NULL;
/**
* Your reference number for the message.
*
* Assists you track the message status. This parameter is optional and can be
* up to 20 characters.
*/
public $ref = '';
/**
* Maximum number of characters that can be inlcuded in a single SMS.
*
* @see $this->maxsplit
*/
const MAX_CHARS_PER_MESSAGE_SINGLE = 160;
/**
* Maximum number of characters that can be included in each SMS when sending
* multipart SMSs.
*
* @see $this->maxsplit
*/
const MAX_CHARS_PER_MESSAGE_MULTI = 153;
/**
* Maximum number of SMSs that can be part of a multipart SMS.
*
* @see $this->maxsplit
*/
const MAX_SMS_PER_MULTIPART = 7;
/**
* Maximum number of characters that can be included in the sender string.
*
* @see $this->sender
*/
const MAX_CHARS_SENDER = 11;
/**
* Constructor
*/
public function __construct($username, $password) {
$this->setAuthentication($username, $password);
}
/**
* Helper method to set authentication details.
*
* @param string $username
* @param string $password
*/
public function setAuthentication($username, $password) {
$this->api_username = $username;
$this->api_password = $password;
}
/**
* Helper method which adds a new recipient to the SMS.
*
* The numbers can be in the format:
* - 04xxxxxxxx (Australian format)
* - 614xxxxxxxx (International format without a preceding +)
* - 4xxxxxxxx (missing leading 0)
* SMS Broadcast recommend using the international format, but your messages
* will be accepted in any of the above formats. The numbers should contain
* only numbers, with no spaces or other characters.
*
* @param string $number
* Phone number of the new recipient
*/
public function addRecipient($number) {
$this->recipients[] = $number;
}
/**
* Executes sending of an SMS.
*
* @return array
* Response from SMS gateway. Contains response for each receiving address.
* Array (
* Array (
* status: Status of the SMS send. Possible values:
* - OK : This message was accepted.
* - BAD: This message was invalid. (eg, invalid phone number)
* receiving_number: The receiving mobile number.
* response: Will display our reference number for the SMS message, or
* the reason for a failed SMS message.
* )
* )
*
* @throws Exception
*
* @see https://www.smsbroadcast.com.au/ajax/apiIn.php
*
* Usage example
* @code
* $api = new smsbroadcastau($username, $password);
* $api->addRecipient('0400000000');
* $api->message = 'Message to send to recipients';
* $api->from = 'SMS API';
* $api->ref = 'identifier';
* $api->send();
* @endcode
*/
public function send() {
$vars = array(
'username' => $this->api_username,
'password' => $this->api_password,
'to' => $this->recipients,
'from' => $this->sender,
'message' => $this->message,
'ref' => $this->ref,
);
// Automatically detect the maxsplit value if required.
if (is_null($this->maxsplit)) {
$message_length = strlen($vars['message']);
if ($message_length <= self::MAX_CHARS_PER_MESSAGE_SINGLE) {
$vars['maxsplit'] = 1;
}
else {
// API documentation states multi-part SMSs are limited to 153 chars.
$vars['maxsplit'] = ceil($message_length / self::MAX_CHARS_PER_MESSAGE_MULTI);
}
}
else {
$vars['maxsplit'] = $this->maxsplit;
}
// Basic validation on the authentication details and POST data.
foreach ($vars as $key => $value) {
switch ($key) {
case 'to':
if (empty($value)) {
throw new Exception('No recipients specified.');
}
break;
case 'from':
if (strlen($value) > self::MAX_CHARS_SENDER) {
throw new Exception('From string must be 11 characters or less.');
}
break;
case 'multisplit':
// Ensure we don't attempt to send multi-part SMS longer than 7 long.
if ($value > self::MAX_SMS_PER_MULTIPART) {
$args = array(
'!chars' => $message_length,
'!multisplit' => $value,
);
throw new Exception(strtr('Can not send a multi-part message longer than 7 SMSs. Attempted to send !chars characters over !multisplit messages.', $args));
}
break;
}
}
$retval = $this->executeApiRequest($vars);
$data = array();
foreach ($retval as $i => $line) {
list($status, $receiving_number, $response) = $line;
$data[$i] = array(
'status' => trim($status),
'receiving_number' => trim($receiving_number),
'response' => trim($response),
);
}
return $data;
}
/**
* Checks the account SMS balance.
*
* @return int
* Number of SMS credits remaining on the account.
*
* @throws Exception
*
* @see https://www.smsbroadcast.com.au/ajax/apiIn.php
*/
public function checkBalance() {
$vars = array(
'username' => $this->api_username,
'password' => $this->api_password,
'action' => 'balance',
);
$retval = $this->executeApiRequest($vars);
list(, $response) = array_values(reset($retval));
return (int) $response;
}
/**
* Helper method to execute an API request.
*
* @param array $vars
* Data to POST to SMS gateway API endpoint.
*
* @return array
* Response from SMS gateway.
*/
public function executeApiRequest($vars) {
// Basic validation on the authentication details
foreach ($vars as $key => $value) {
switch ($key) {
case 'username':
case 'password':
if (empty($value)) {
throw new Exception('API username or password not specified.');
}
break;
}
}
$data = $this->preparePostData($vars);
$retval = $this->executePostRequest($data);
list($status, $response) = explode(':', $retval);
if ($status == 'ERROR') {
throw new Exception(strtr('There was an error with this request: !error.', array('!error' => $response)));
}
$data = array();
$lines = explode("\n", $retval);
foreach (array_filter($lines) as $i => $line) {
$line = trim($line);
$data[$i] = explode(':', $line);
}
return $data;
}
/**
* Protected helper which makes basic associative array suitable for POST.
*
* @param array $data
* Associative array containing data to POST to SMS gateway.
*
* @return string
* URL encoded POST data.
*/
protected function preparePostData($data) {
$post_data = array();
foreach ($data as $key => $value) {
switch ($key) {
case 'to':
// Support multiple phone numbers.
$value = implode(',', array_unique($value));
break;
}
$post_data[] = $key . '=' . rawurlencode($value);
}
return implode('&', $post_data);
}
/**
* Protected helper which executes the cURL POST request for API calls.
*
* @param string $data
* Data to POST.
*
* @return string $retval
* Raw response from SMS gateway.
*/
protected function executePostRequest($data) {
$ch = curl_init($this->api_endpoint);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$retval = curl_exec($ch);
curl_close($ch);
return $retval;
}
}