-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClickATell.php
141 lines (122 loc) · 4.05 KB
/
ClickATell.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
<?php
/**
* Description of ClickATell Class
*
* It deals with www.clickatell.com API.
*
* @author Syed Abidur Rahman <[email protected]>
*/
class ClickATell
{
private $baseUrl = "http://api.clickatell.com";
private $smsAccount = '[email protected]';
private $username = '';
private $password = '';
private $apiId = '';
private $contacts = array();
private $text = '';
public function __construct($credentials)
{
$this->username = $credentials['username'];
$this->password = $credentials['password'];
$this->apiId = $credentials['apiId'];
}
public function checkCredentialsNotEmpty()
{
if (empty($this->username) || empty($this->password) || empty($this->apiId)) {
return false;
}
return true;
}
public function authenticateCredentials($returnSession = false)
{
$url = "{$this->baseUrl}/http/auth?user={$this->username}&password={$this->password}&api_id={$this->apiId}";
try {
$response = file($url); // do auth call
} catch(Exception $e){
$response = array('Not Ok');
};
// explode our response. return string is on first line of the data returned
$gatewaySession = explode(":", $response[0]);
if ($gatewaySession[0] != "OK") {
return false;
}
return $returnSession ? trim($gatewaySession[1]) : true; // remove any whitespace
}
public function setContacts(array $contacts)
{
$this->contacts = $contacts;
}
public function setSMSText($smsText = '')
{
$this->text = $smsText;
}
public function sendSMS()
{
if ($this->checkCredentialsNotEmpty()) {
if ($sessionId = $this->authenticateCredentials(true)) {
return $this->dealsWithSendingSMS($sessionId);
} else {
return array(
'type' => 'error',
'msg' => 'Authentication of the credential has been failed.'
);
}
} else {
return array(
'type' => 'error',
'msg' => 'Credential information is missing.'
);
}
}
public function getEmailDetail()
{
$contactStr = '';
foreach ($this->contacts AS $contact) {
empty($contact) || $contactStr .= "to:{$contact}\n\r";
}
$mailBody = <<<EOF
api_id:{$this->apiId}
user:{$this->username}
password:{$this->password}
text:{$this->text}
{$contactStr}
mo=1
from=13476622235
EOF;
return array(
'to' => $this->smsAccount,
'subject' => '',
'message' => $mailBody
);
}
private function dealsWithSendingSMS($sessionId)
{
$contacts = implode(',', $this->contacts);
$text = urlencode($this->text);
$smsUrl = "{$this->baseUrl}/http/sendmsg?session_id={$sessionId}&to={$contacts}&text={$text}&mo=1&from=13476622235";
$response = file($smsUrl);
$count = 0;
$resultOfSendingSMS = array();
foreach ($response AS $currentResponse) {
$send = explode(":", $currentResponse);
if ($send[0] == "ID") {
$resultOfSendingSMS[] = array('contact' => $this->contacts[$count++], 'type' => 'success');
} else {
$error = explode(', ', $send[1]);
if ($error[0] == '301') {
$resultOfSendingSMS[] = array(
'contact' => $this->contacts[$count++],
'type' => 'error',
'msg' => 'no credit left.');
} elseif ($error[0] == 114) {
$resultOfSendingSMS[] = array(
'contact' => $this->contacts[$count++],
'type' => 'error',
'msg' => 'incorrect number perhaps.');
}
}
}
return $resultOfSendingSMS;
}
}