-
Notifications
You must be signed in to change notification settings - Fork 43
/
Osms.php
382 lines (330 loc) · 11 KB
/
Osms.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
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
namespace Osms;
class Osms
{
const BASE_URL = 'https://api.orange.com';
/**
* Client Identifier. Unique ID provided by the Orange backend server to identify
* your application.
*
* @var string
*/
protected $clientId = '';
/**
* Client Secret. Used to sign/crypt the requests.
*
* @var string
*/
protected $clientSecret = '';
/**
* The Token will be used for all further API calls.
*
* @var string
*/
protected $token = '';
/**
* cURL option for whether to verify the peer's certificate or not.
*
* @var bool
*/
protected $verifyPeerSSL = true;
/**
* Creates a new Osms instance. If the user doesn't know his token or doesn't have a
* token yet, he can leave $token empty and retrieve a token with
* getTokenFromConsumerKey() method later.
*
* @param array $config An associative array that can contain clientId, clientSecret,
* token, and verifyPeerSSL
*
* @return void
*/
public function __construct($config = array())
{
if (array_key_exists('clientId', $config)) {
$this->clientId = $config['clientId'];
}
if (array_key_exists('clientSecret', $config)) {
$this->clientSecret = $config['clientSecret'];
}
if (array_key_exists('token', $config)) {
$this->token = $config['token'];
}
if (array_key_exists('verifyPeerSSL', $config)) {
$this->verifyPeerSSL = $config['verifyPeerSSL'];
} else {
$this->verifyPeerSSL = true;
}
}
/**
* Retrieves a token from Orange server, that will be used for all further API calls.
*
* @return array
*/
public function getTokenFromConsumerKey()
{
$url = self::BASE_URL . '/oauth/v2/token';
$credentials = $this->getClientId() . ':' . $this->getClientSecret();
$headers = array('Authorization: Basic ' . base64_encode($credentials));
$args = array('grant_type' => 'client_credentials');
$response = $this->callApi($headers, $args, $url, 'POST', 200);
if (!empty($response['access_token'])) {
$this->setToken($response['access_token']);
}
return $response;
}
/**
* Sends SMS.
*
* @param string $senderAddress The receiver address in this format:
* "tel:+22500000000"
* @param string $receiverAddress The receiver address in this format:
* "tel:+22500000000"
* @param string $message The content of the SMS, must not exceed
* 160 characters
* @param string $senderName The sender name
*
* @return array
*/
public function sendSms(
$senderAddress,
$receiverAddress,
$message,
$senderName = ''
) {
$url = self::BASE_URL . '/smsmessaging/v1/outbound/' . urlencode($senderAddress)
. '/requests';
$headers = array(
'Authorization: Bearer ' . $this->getToken(),
'Content-Type: application/json'
);
if (!empty($senderName)) {
$args = array(
'outboundSMSMessageRequest' => array(
'address' => $receiverAddress,
'senderAddress' => $senderAddress,
'senderName' => urlencode($senderName),
'outboundSMSTextMessage' => array(
'message' => $message
)
)
);
} else {
$args = array(
'outboundSMSMessageRequest' => array(
'address' => $receiverAddress,
'senderAddress' => $senderAddress,
'outboundSMSTextMessage' => array(
'message' => $message
)
)
);
}
return $this->callApi($headers, $args, $url, 'POST', 201, true);
}
/**
* Lists SMS usage statistics per application.
*
* @param array $args An associative array to filter the results, containing
* country (the international 3 digits country code) and/or
* appid (you can retrieve your application ID from your
* dashboard application)
*
* @return array
*/
public function getAdminStats($args = null)
{
$url = self::BASE_URL . '/sms/admin/v1/statistics';
$headers = array('Authorization: Bearer ' . $this->getToken());
return $this->callApi($headers, $args, $url, 'GET', 200);
}
/**
* Displays how many SMS you can still send.
*
* @param string $country The country to filter on (the international 3 digits
* country)
*
* @return array
*/
public function getAdminContracts($country = '')
{
$url = self::BASE_URL . '/sms/admin/v1/contracts';
$headers = array('Authorization: Bearer ' . $this->getToken());
$args = null;
if (!empty($country)) {
$args = array('country' => $country);
}
return $this->callApi($headers, $args, $url, 'GET', 200);
}
/**
* Lists your purchase history.
*
* @param string $country The country to filter on (the international 3 digits
* country)
*
* @return array
*/
public function getAdminPurchasedBundles($country = '')
{
$url = self::BASE_URL . '/sms/admin/v1/purchaseorders';
$headers = array('Authorization: Bearer ' . $this->getToken());
$args = null;
if (!empty($country)) {
$args = array('country' => $country);
}
return $this->callApi($headers, $args, $url, 'GET', 200);
}
/**
* Calls API Endpoints.
*
* @param array $headers An array of HTTP header fields to set
* @param array $args The data to send
* @param string $url The URL to fetch
* @param string $method Whether to do a HTTP POST or a HTTP GET
* @param int $successCode The HTTP code that will be returned on
* success
* @param bool $jsonEncodeArgs Whether or not to json_encode $args
*
* @return array Contains the results returned by the endpoint or an error
* message
*/
public function callApi(
$headers,
$args,
$url,
$method,
$successCode,
$jsonEncodeArgs = false
) {
$ch = curl_init();
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
if (!empty($args)) {
if ($jsonEncodeArgs === true) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args));
}
}
} else /* $method === 'GET' */ {
if (!empty($args)) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($args));
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($this->getVerifyPeerSSL() === false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
// Make sure we can access the response when we execute the call
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
if ($data === false) {
return array('error' => 'API call failed with cURL error: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response = json_decode($data, true);
$jsonErrorCode = json_last_error();
if ($jsonErrorCode !== JSON_ERROR_NONE) {
return array(
'error' => 'API response not well-formed (json error code: '
. $jsonErrorCode . ')'
);
}
if ($httpCode !== $successCode) {
$errorMessage = '';
if (!empty($response['error_description'])) {
$errorMessage = $response['error_description'];
} elseif (!empty($response['error'])) {
$errorMessage = $response['error'];
} elseif (!empty($response['description'])) {
$errorMessage = $response['description'];
} elseif (!empty($response['message'])) {
$errorMessage = $response['message'];
} elseif (!empty($response['requestError']['serviceException'])) {
$errorMessage = $response['requestError']['serviceException']['text']
. ' ' . $response['requestError']['serviceException']['variables'];
} elseif (!empty($response['requestError']['policyException'])) {
$errorMessage = $response['requestError']['policyException']['text']
. ' ' . $response['requestError']['policyException']['variables'];
}
return array('error' => $errorMessage);
}
return $response;
}
/**
* Gets the Cliend ID.
*
* @return string
*/
public function getClientId()
{
return $this->clientId;
}
/**
* Sets the Client ID.
*
* @param string $clientId the Client ID
*/
public function setClientId($clientId)
{
$this->clientId = $clientId;
}
/**
* Gets the Client Secret.
*
* @return string
*/
public function getClientSecret()
{
return $this->clientSecret;
}
/**
* Sets the Client Secret.
*
* @param string $clientSecret the Client Secret
*/
public function setClientSecret($clientSecret)
{
$this->clientSecret = $clientSecret;
}
/**
* Gets the (local/current) Token.
*
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* Sets the Token value.
*
* @param string $token the token
*/
public function setToken($token)
{
$this->token = $token;
}
/**
* Gets the CURLOPT_SSL_VERIFYPEER value.
*
* @return bool
*/
public function getVerifyPeerSSL()
{
return $this->verifyPeerSSL;
}
/**
* Sets the CURLOPT_SSL_VERIFYPEER value.
*
* @param bool $verifyPeerSSL FALSE to stop cURL from verifying the
* peer's certificate. TRUE otherwise.
*/
public function setVerifyPeerSSL($verifyPeerSSL)
{
$this->verifyPeerSSL = $verifyPeerSSL;
}
}