|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BongaTech SMS Client Library for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2017 |
|
6
|
|
|
* @author Vincent Mosoti <[email protected]> |
|
7
|
|
|
* @license https://github.com/VMosoti/bongatech-sms/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace VMosoti\BongaTech; |
|
11
|
|
|
|
|
12
|
|
|
use VMosoti\BongaTech\Exceptions\BongaTechException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class SMS. |
|
16
|
|
|
*/ |
|
17
|
|
|
class SMS |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Version number of the SMS API. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
const VERSION = '2.0.0'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Base URL. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
const BASE_URL = 'http://197.248.4.47/smsapi/'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Send SMS endpoint. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
const SMS_ENDPOINT = 'submit.php'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get Balance endpoint. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
const GET_BALANCE_ENDPOINT = 'balance.php'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* token is generated by md5(password). |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $token; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* timestamp is the current datetime. |
|
56
|
|
|
* |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $timestamp; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* type of SMS to sent. |
|
63
|
|
|
* |
|
64
|
|
|
* @var int message |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $message_type = 0; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* batch type of the SMS being sent. |
|
70
|
|
|
* |
|
71
|
|
|
* @var int batch |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $batch_type; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* sms configurations. |
|
77
|
|
|
* |
|
78
|
|
|
* @array config |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $config; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* end point url. |
|
84
|
|
|
* |
|
85
|
|
|
* @string endpoint |
|
86
|
|
|
*/ |
|
87
|
|
|
protected $endpoint; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* the message(s) being sent (array of messages in case message is different for each user. |
|
91
|
|
|
* |
|
92
|
|
|
* @var array. |
|
93
|
|
|
*/ |
|
94
|
|
|
protected $message; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* the recipients . |
|
98
|
|
|
* |
|
99
|
|
|
* @var array. |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $recipients; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* SMS constructor. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function __construct() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->config = Config::get(); |
|
109
|
|
|
|
|
110
|
|
|
if ( |
|
111
|
|
|
empty($this->config['user_id']) || |
|
112
|
|
|
empty($this->config['password']) || |
|
113
|
|
|
empty($this->config['sender_id']) |
|
114
|
|
|
) { |
|
115
|
|
|
throw new BongaTechException('Please ensure that all configuration variables have been set.'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->setTimestamp(); |
|
119
|
|
|
$this->setToken(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* set the timestamp. |
|
124
|
|
|
*/ |
|
125
|
|
|
private function setTimestamp() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->timestamp = date('YmdHis'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* set the token. |
|
132
|
|
|
*/ |
|
133
|
|
|
private function setToken() |
|
134
|
|
|
{ |
|
135
|
|
|
$this->token = md5($this->config['password']); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* invoke if SMS being sent is of type subscribable. |
|
140
|
|
|
* |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
|
|
public function subscribable() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->message_type = MessageType::SUBSCIBABLE; |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* invoke if SMS being sent is of type on demand. |
|
152
|
|
|
* |
|
153
|
|
|
* @return $this |
|
154
|
|
|
*/ |
|
155
|
|
|
public function onDemand() |
|
156
|
|
|
{ |
|
157
|
|
|
$this->message_type = MessageType::ON_DEMAND; |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* invoke if SMS being sent is of type bulk SMS. This will be the common one. |
|
164
|
|
|
* |
|
165
|
|
|
* @return $this |
|
166
|
|
|
*/ |
|
167
|
|
|
public function bulk() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->message_type = MessageType::BULK; |
|
170
|
|
|
|
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* invoke if SMS is being sent to a single recipient. |
|
176
|
|
|
* |
|
177
|
|
|
* @return $this |
|
178
|
|
|
*/ |
|
179
|
|
|
public function toOne() |
|
180
|
|
|
{ |
|
181
|
|
|
$this->batch_type = BatchType::NOT_BATCH; |
|
182
|
|
|
|
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* invoke if SMS is being sent to a different recipients, or if each recipient receives custom sms. |
|
188
|
|
|
* |
|
189
|
|
|
* @return $this |
|
190
|
|
|
*/ |
|
191
|
|
|
public function toMany() |
|
192
|
|
|
{ |
|
193
|
|
|
$this->batch_type = BatchType::BATCH; |
|
194
|
|
|
|
|
195
|
|
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param $recipients |
|
200
|
|
|
* @param $message |
|
201
|
|
|
* |
|
202
|
|
|
* @throws BongaTechException |
|
203
|
|
|
* |
|
204
|
|
|
* @return mixed |
|
205
|
|
|
*/ |
|
206
|
|
|
public function send($recipients, $message) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->recipients = $recipients; |
|
209
|
|
|
$this->message = $message; |
|
210
|
|
|
$this->endpoint = self::BASE_URL.self::SMS_ENDPOINT; |
|
211
|
|
|
|
|
212
|
|
|
if ($this->batch_type === BatchType::NOT_BATCH) { |
|
213
|
|
View Code Duplication |
if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) == 1) { |
|
214
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) == 1) { |
|
215
|
|
|
$response = $this->sendForNonBatch($this->buildSendObject($this->recipients, $this->message)); |
|
216
|
|
|
} else { |
|
217
|
|
|
throw new BongaTechException('The recipient MUST be an array of depth 2 and count should not be more than 1'); |
|
218
|
|
|
} |
|
219
|
|
|
} else { |
|
220
|
|
|
throw new BongaTechException('Message should be provided as an array whose depth is 2 and count should equal 1'); |
|
221
|
|
|
} |
|
222
|
|
|
} elseif ($this->batch_type === BatchType::BATCH) { |
|
223
|
|
View Code Duplication |
if (is_array($this->message) && array_depth($this->message) == 2 && count($this->message) > 0) { |
|
224
|
|
|
if (is_array($this->recipients) && array_depth($this->recipients) == 2 && count($this->recipients) > 1) { |
|
225
|
|
|
$response = $this->sendForBatch($this->buildSendObject($this->recipients, $this->message)); |
|
226
|
|
|
} else { |
|
227
|
|
|
throw new BongaTechException('The recipients MUST be an array of depth 2 and count should be more than 1'); |
|
228
|
|
|
} |
|
229
|
|
|
} else { |
|
230
|
|
|
throw new BongaTechException('Message should be provided as an array whose depth should be 2'); |
|
231
|
|
|
} |
|
232
|
|
|
} else { |
|
233
|
|
|
throw new BongaTechException('Message Batch Type has not been set.'); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return $response; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* build the send object. |
|
241
|
|
|
* |
|
242
|
|
|
* @param recipients |
|
243
|
|
|
* @param $messages |
|
244
|
|
|
* |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
private function buildSendObject($recipients, $messages) |
|
248
|
|
|
{ |
|
249
|
|
|
$body = [ |
|
250
|
|
|
'AuthDetails' => [ |
|
251
|
|
|
[ |
|
252
|
|
|
'UserID' => $this->config['user_id'], |
|
253
|
|
|
'Token' => $this->token, |
|
254
|
|
|
'Timestamp' => $this->timestamp, |
|
255
|
|
|
|
|
256
|
|
|
], |
|
257
|
|
|
], |
|
258
|
|
|
'MessageType' => [ |
|
259
|
|
|
(string) $this->message_type, |
|
260
|
|
|
], |
|
261
|
|
|
'BatchType' => [ |
|
262
|
|
|
(string) $this->batch_type, |
|
263
|
|
|
], |
|
264
|
|
|
'SourceAddr' => [ |
|
265
|
|
|
(string) $this->config['sender_id'], |
|
266
|
|
|
], |
|
267
|
|
|
'MessagePayload' => $messages, |
|
268
|
|
|
'DestinationAddr' => $recipients, |
|
269
|
|
|
'DeliveryRequest' => [ |
|
270
|
|
|
[ |
|
271
|
|
|
'EndPoint' => $this->config['callback_url'], |
|
272
|
|
|
'Correlator' => (string) mt_rand(), |
|
273
|
|
|
], |
|
274
|
|
|
], |
|
275
|
|
|
]; |
|
276
|
|
|
|
|
277
|
|
|
return $body; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* send a message to a single recipient. |
|
282
|
|
|
* |
|
283
|
|
|
* @param $body |
|
284
|
|
|
* |
|
285
|
|
|
* @return Response |
|
286
|
|
|
*/ |
|
287
|
|
|
private function sendForNonBatch($body) |
|
288
|
|
|
{ |
|
289
|
|
|
$request = new Request($this->endpoint, $body); |
|
290
|
|
|
$response = $request->sendSMS(); |
|
291
|
|
|
|
|
292
|
|
|
return new Response($response->body[0]); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* send batch. 1) same message to many recipients 2) different messages to many recipients. |
|
297
|
|
|
* |
|
298
|
|
|
* @param $body |
|
299
|
|
|
* |
|
300
|
|
|
* @return array |
|
301
|
|
|
*/ |
|
302
|
|
|
private function sendForBatch($body) |
|
303
|
|
|
{ |
|
304
|
|
|
$request = new Request($this->endpoint, $body); |
|
305
|
|
|
$response = $request->sendSMS(); |
|
306
|
|
|
|
|
307
|
|
|
$responses = []; |
|
308
|
|
|
$response_count = count($response->body); |
|
309
|
|
|
|
|
310
|
|
|
for ($i = 0; $i < $response_count; $i++) { |
|
311
|
|
|
$res = new Response($response->body[$i]); |
|
312
|
|
|
$responses[] = $res; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
return $responses; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
public static function getBalance() |
|
319
|
|
|
{ |
|
320
|
|
|
$config = Config::get(); |
|
321
|
|
|
|
|
322
|
|
|
$endpoint = self::BASE_URL.self::GET_BALANCE_ENDPOINT.'?UserID='.$config['user_id'].'&Token='.md5($config['password']); |
|
323
|
|
|
|
|
324
|
|
|
$request = new Request($endpoint); |
|
325
|
|
|
$response = $request->getBalance(); |
|
326
|
|
|
|
|
327
|
|
|
return new Response($response->body); |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|