-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAPIClient2.php
More file actions
665 lines (606 loc) · 16.6 KB
/
APIClient2.php
File metadata and controls
665 lines (606 loc) · 16.6 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
<?php
class transmitsmsAPI
{
public $url='https://api.transmitsms.com/';
protected $version=2;
protected $authHeader;
protected $responseRawData;
protected $responseStatus;
public function __construct($key, $secret)
{
$this->authHeader=array('Authorization: Basic '.base64_encode($key.':'.$secret));
}
protected function generateError($code, $description)
{
$error=new stdClass();
$error->error = new stdClass();
$error->error->code=$code;
$error->error->description=$description;
return $error;
}
protected function getRequestURL($method)
{
return $this->url.'/'.$this->version.'/'.$method.'.json';
}
protected function request($method, $params=array())
{
$requestUrl=$this->getRequestURL($method);
$ch = curl_init($requestUrl);
if (! $ch) {
return $this->generateError('REQUEST_FAILED' ,"Error connecting to the server {$requestUrl} : ". curl_errno($ch) .':'. curl_error($ch));
}
$urlInfo = parse_url($requestUrl);
$port = (preg_match("/https|ssl/i", $urlInfo["scheme"])) ? 443 : 80;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_USERAGENT, "transmitsmsAPI v." . $this->version);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->authHeader);
$this->responseRawData = curl_exec($ch);
if (! $this->responseRawData) {
return $this->generateError('REQUEST_FAILED' ,"Problem executing request, try changing above set options and re-requesting : ".curl_errno($ch) .':' .curl_error($ch));
}
$this->responseStatus=curl_getinfo($ch, CURLINFO_HTTP_CODE);
return json_decode($this->responseRawData);//, false, 512, JSON_BIGINT_AS_STRING);
}
protected function handleResponse($response)
{
if($response===null)
return $this->generateError("INVALID_RESPONSE", "Invalid response, received data: ".$this->responseRawData);
//possible checks for login failure and other common mistakes
return $response;
}
protected function indexCustomFields(&$params, $fields)
{
if(!count($fields))
return;
if(isset($fields[0]))
{
// this is not an associative array, we iterate and indexify from 1 to 10
$fieldIndex=1;
foreach($fields as $field)
{
$params["field_{$fieldIndex}"]=$field;
$fieldIndex++;
}
}
else
{
// this is an associative array, we iterate and keep the indexes
foreach($fields as $fieldIndex=>$field)
{
$params["field_{$fieldIndex}"]=$field;
}
}
}
protected function prepareFieldsForEdit(&$params)
{
foreach ($params as $key=>$value) {
if($value===null)
unset($params[$key]);
}
}
/**
* Send SMS messages.
*
* @param string $message
* @param string $to - required if list_id is not set
* @param string $from
* @param datetime $send_at
* @param int $list_id - required if to is not set
* @param string $dlr_callback
* @param string $reply_callback
* @param int $validity
*
*/
public function sendSms($message, $to='', $from='', $send_at='', $list_id=0, $dlr_callback='', $reply_callback='', $validity=0, $replies_to_email='', $tracked_link_url='')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('send-sms', $params));
}
/**
* Get data about a sent message.
*
* @param int $message_id
*
*/
public function getSms($message_id)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-sms', $params));
}
/**
* Get sent messages.
*
* @param int $message_id
* @param int $page
* @param int $max
* @param string $optouts can be 'only', 'omit', 'include'
*
*/
public function getSmsSent($message_id, $page=1, $max=10, $optouts='include', $delivery='all')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-sms-sent', $params));
}
/**
* Get SMS responses.
*
* @param int $message_id
* @param int $keyword_id
* @param string $keyword
* @param string $number
* @param string $msisdn
* @param int $page
* @param int $max
*
*/
public function getSmsResponses($message_id, $keyword_id=0, $keyword='', $number='', $msisdn='', $page=1, $max=10, $include_original=false)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-sms-responses', $params));
}
/**
* Get SMS responses for user
*
* @param datetime $start
* @param datetime $end
* @param int $page
* @param int $max
* @param string $keywords
*
*/
public function getUserSmsResponses($start=null, $end=null, $page=1, $max=10, $keywords='both', $number = '', $include_original=false)
{
$params = get_defined_vars();
$this->prepareFieldsForEdit($params);
return $this->handleResponse($this->request('get-user-sms-responses', $params));
}
/**
* Get SMS sent by user in certain time frame
*
* @param datetime $start
* @param datetime $end
* @param string $msisdn
* @param int $page
* @param int $max
*
*/
public function getUserSmsSent($start=null, $end=null, $msisdn='', $page=1, $max=10)
{
$params = get_defined_vars();
$this->prepareFieldsForEdit($params);
return $this->handleResponse($this->request('get-user-sms-sent', $params));
}
/**
* Cancel a scheduled SMS
*
* @param int $id
*
*/
public function cancelSms($id)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('cancel-sms', $params));
}
/**
* Get information about a list and its members.
*
* @param int $list_id
* @param int $page
* @param int $max
* @param string $members can be 'active', 'inactive', 'all', 'none'
*
*/
public function getList($list_id, $page=1, $max=10, $members='active')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-list', $params));
}
/**
* Get the metadata of your lists.
* @param int $page
* @param int $max
*
*/
public function getLists($page=1, $max=10)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-lists', $params));
}
/**
* Create a new list.
*
* @param string $name
* @param array $fields
*
*/
public function addList($name, $fields=array())
{
$params['name']=$name;
$this->indexCustomFields($params, $fields);
return $this->handleResponse($this->request('add-list', $params));
}
/**
* Delete a list and its members.
*
* @param integer $list_id
*
*/
public function removeList($list_id)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('remove-list', $params));
}
/**
* Add a member to a list.
*
* @param int $list_id
* @param string $msisdn
* @param string $first_name
* @param string $last_name
* @param array $fields
*
*/
public function addToList($list_id, $msisdn, $first_name='', $last_name='', $fields=array(), $countrycode=NULL)
{
$params=get_defined_vars();
unset($params['fields']);
$this->indexCustomFields($params, $fields);
return $this->handleResponse($this->request('add-to-list', $params));
}
/**
* Edit a list member.
*
* @param int $list_id
* @param string $msisdn
* @param string $first_name
* @param string $last_name
* @param array $fields
*
*/
public function editListMember($list_id, $msisdn, $first_name=null, $last_name=null, $fields=array())
{
$params=get_defined_vars();
unset($params['fields']);
$this->indexCustomFields($params, $fields);
$this->prepareFieldsForEdit($params);
return $this->handleResponse($this->request('edit-list-member', $params));
}
/**
* Remove a member from one list or all lists.
*
* @param int $list_id
* @param string $msisdn
*
*/
public function deleteFromList($list_id, $msisdn)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('delete-from-list', $params));
}
/**
* Opt-out a member from one list or all lists.
*
* @param int $list_id
* @param string $msisdn
*
*/
public function optoutListMember($list_id, $msisdn)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('optout-list-member', $params));
}
/**
* Get leased number details
*
* @param string $number
*
*/
public function getNumber($number)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-number', $params));
}
/**
* Get a list of numbers.
*
* @param int $page
* @param int $max
* @param string $filter
*
*/
public function getNumbers($page=1, $max=10, $filter='owned')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-numbers', $params));
}
/**
* Lease a response number.
*
* @param string $number
*
*/
public function leaseNumber($number='', $url='')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('lease-number', $params));
}
/**
* Edit options of existing number.
*
* @param string $number
* @param string $forward_email
* @param string $forward_sms
* @param string $forward_url
* @param int $list_id
* @param string $welcome_message
* @param string $members_message
*
*/
public function editNumberOptions($number, $forward_email='', $forward_sms='', $forward_url='', $list_id=0, $welcome_message='', $members_message='')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('edit-number-options', $params));
}
/**
* Get a client.
*
* @param int $client_id
*
*/
public function getClient($client_id)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-client', $params));
}
/**
* Get a list of clients.
*
* @param int $page
* @param int $max
*
*/
public function getClients($page=1, $max=10)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-clients', $params));
}
/**
* Add a new client
*
* @param string $name
* @param string $email
* @param string $password
* @param string $msisdn
* @param string $contact
* @param string $timezone
* @param bool $client_pays
* @param float $sms_margin
*
*/
public function addClient($name, $email, $password, $msisdn, $contact='', $timezone='', $client_pays=true, $sms_margin=0.0)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('add-client', $params));
}
/**
* Edit a client
*
* @param int $id
* @param string $name
* @param string $email
* @param string $password
* @param string $msisdn
* @param string $contact
* @param string $timezone
* @param bool $client_pays
* @param float $sms_margin
*
*/
public function editClient($client_id, $name=null, $email=null, $password=null, $msisdn=null, $contact=null, $timezone=null, $client_pays=null, $sms_margin=null)
{
$params = get_defined_vars();
$this->prepareFieldsForEdit($params);
return $this->handleResponse($this->request('edit-client', $params));
}
/**
* Add a keyword to your existing response number.
*
* @param string $keyword
* @param string $number
* @param string $reference
* @param int $list_id
* @param string $welcome_message
* @param string $members_message
* @param bool $activate
* @param string $forward_url
* @param string $forward_email
* @param string $forward_sms
*
*/
public function addKeyword($keyword, $number, $reference='', $list_id=0, $welcome_message='', $members_message='', $activate=true, $forward_url='', $forward_email='', $forward_sms='')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('add-keyword', $params));
}
/**
* Edit an existing keyword.
*
* @param string $keyword
* @param string $number
* @param string $reference
* @param int $list_id
* @param string $welcome_message
* @param string $members_message
* @param string $status
* @param string $forward_url
* @param string $forward_email
* @param string $forward_sms
*
*/
public function editKeyword($keyword, $number, $reference=null, $list_id=null, $welcome_message=null, $members_message=null, $status=null, $forward_url=null, $forward_email=null, $forward_sms=null)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('edit-keyword', $params));
}
/**
* Get a list of existing keywords.
*
* @param string $number
* @param int $page
* @param int $max
*
*/
public function getKeywords($number='',$page=1, $max=10)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-keywords', $params));
}
/**
* Get a list of transactions for a client.
*
* @param int $client_id
* @param datetime $start
* @param datetime $end
*
*/
public function getTransactions($client_id, $start=null, $end=null, $page=1, $max=10)
{
$params = get_defined_vars();
$this->prepareFieldsForEdit($params);
return $this->handleResponse($this->request('get-transactions', $params));
}
/**
* Get a transaction.
*
* @param int $id
*
*/
public function getTransaction($id)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-transaction', $params));
}
/**
* Register an email address for Email to SMS.
*
* @param string $email
* @param int $max_sms
* @param string $number
*
*/
public function addEmail($email, $max_sms=1, $number='')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('add-email', $params));
}
/**
* Remove an email address from Email to SMS.
*
* @param string $email
*
*/
public function deleteEmail($email)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('delete-email', $params));
}
/**
* Get active user's balance
*
*/
public function getBalance()
{
return $this->handleResponse($this->request('get-balance'));
}
public function formatNumber($msisdn, $countrycode)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('format-number', $params));
}
/**
* Add contacts in bulk
* @param string $name
* @param string $file_url
* @param array $fields
* @param string $countrycode
*
*/
public function addContactsBulk($name, $file_url, $fields = [], $countrycode = '')
{
$params = get_defined_vars();
$this->indexCustomFields($params, $fields);
return $this->handleResponse($this->request('add-contacts-bulk', $params));
}
/**
* Check add contacts bulk progress
* @param integer $list_id
*
*/
public function addContactsBulkProgress($list_id)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('add-contacts-bulk-progress', $params));
}
/**
* Get contact details
* @param integer $list_id
* @param string $msisdn
*
*/
public function getContact($list_id, $msisdn)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-contact', $params));
}
/**
* Get link hits report
* @param integer $message_id
* @param integer $page
* @param integer $max
*
*/
public function getLinkHits($message_id, $page = 1, $max = 10)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-link-hits', $params));
}
/**
* Get sms delivery status
* @param integer $message_id
* @param string $msisdn
*
*/
public function getSmsDeliveryStatus($message_id, $msisdn)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('get-sms-delivery-status', $params));
}
/**
* Add credit
* @param float $amount
* @param string $creditcard_id
*/
public function addCredit($amount = 0, $creditcard_id = '')
{
$params = get_defined_vars();
return $this->handleResponse($this->request('add-credit', $params));
}
/**
* Add credit card
* @param string $name
* @param integer $number
* @param integer $expiry_month
* @param integer $expiry_year
* @param integer $cvv
*/
public function addCard($name, $number, $expiry_month, $expiry_year, $cvv)
{
$params = get_defined_vars();
return $this->handleResponse($this->request('add-card', $params));
}
}
?>