-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsmsified.class.php
More file actions
176 lines (150 loc) · 3.94 KB
/
smsified.class.php
File metadata and controls
176 lines (150 loc) · 3.94 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
<?php
/**
*
* A PHP class for interacting with the SMSified API.
*
*/
class SMSified {
// Private class members.
private $base_url = 'https://api.smsified.com/v1/';
private $username;
private $password;
/**
*
* Class constructor
* @param string $username
* @param string $password
*/
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
}
/**
*
* Send an outbound SMS message.
* @param string $senderAddress
* @param string $address
* @param string $message
* @param string $notifyURL
*/
public function sendMessage($senderAddress, $address, $message, $notifyURL=NULL) {
$message = urlencode($message);
$url = $this->base_url . "smsmessaging/outbound/$senderAddress/requests?address=$address&message=$message";
if($notifyURL) {
$url .= "¬ifyURL=$notifyURL";
}
return self::makeAPICall('POST', $url);
}
/**
*
* Check the delivery status of an outbound SMS message.
* @param unknown_type $senderAddress
* @param unknown_type $requirestId
*/
public function checkStatus($senderAddress, $requestId) {
$url = $this->base_url . "smsmessaging/outbound/$senderAddress/requests/$requestId/deliveryInfos";
return self::makeAPICall('GET', $url);
}
/**
*
* Create a subscription.
* @param string $senderAddress
* @param string $direction
* @param string $notifyURL
*/
public function createSubscription($senderAddress, $direction, $notifyURL) {
$url = $this->base_url . "smsmessaging/$direction/$senderAddress/subscriptions?notifyURL=$notifyURL";
return self::makeAPICall('POST', $url);
}
/**
*
* View subscrptions
* @param string $senderAddress
* @param string $direction
*/
public function viewSubscriptions($senderAddress, $direction) {
$url = $this->base_url . "smsmessaging/$direction/subscriptions/?senderAddress=$senderAddress";
return self::makeAPICall('GET', $url);
}
/**
*
* Delete an active subscription.
* @param string $subscriptionId
* @param string $direction
*/
public function deleteSubscriptions($subscriptionId, $direction) {
$url = $this->base_url . "smsmessaging/$direction/subscriptions/$subscriptionId";
return self::makeAPICall('DELETE', $url);
}
/**
*
* Get the details of SMS message delivery.
* @param string $messageId
* @param array $params
*/
public function getMessages($messageId=NULL, $params=NULL) {
$url = $this->base_url . "messages/";
if($messageId) {
$url .= "$messageId";
}
else {
$url .= '?';
foreach($params as $key => $value) {
$url .= "$key=$value&";
}
}
return self::makeAPICall('GET', $url);
}
/**
* Method to make REST API call.
*
* @param string $method
* @param string $url
* @param string $payload
* @return string JSON
*/
private function makeAPICall($method, $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
switch($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default:
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
}
$result = curl_exec($ch);
$error = curl_error($ch);
$curl_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($result === false) {
throw new SMSifiedException('An error occurred: '.$error);
} else {
if (substr($curl_http_code, 0, 2) != '20') {
throw new SMSifiedException('An error occurred: http_code: '.$curl_http_code.' error:'.$result);
}
return $result;
}
}
}
/**
*
* A simple class to wrap exceptions.
*
*/
class SMSifiedException extends Exception {}
/**
*
* Helper class with message direction.
*
*/
class MessageDirection {
public static $inbound = 'inbound';
public static $outbound = 'outbound';
}
?>