-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
423 lines (354 loc) · 15.1 KB
/
main.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
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
<?php
/**
*
* SMS Gateway (HTTP) integration with SMSLink.ro
*
* Supports HTTP and HTTPS protocols
* Supports PHP cURL GET, PHP cURL POST and file_get_contents()
*
* System Requirements:
*
* PHP 5 with
* CURL enabled or file_get_contents with allow_url_fopen to be set to 1 in php.ini
*
* Usage:
*
* See Usage Examples for the SMSLinkSMSGateway() class starting on line 354
*
* Get your SMSLink / SMS Gateway Connection ID and Password from
* https://www.smslink.ro/get-api-key/
*
* @version 2.0
* @see https://www.smslink.ro/sms-gateway-documentatie-sms-gateway.html
*
*/
class SMSLinkSMSGateway
{
private $connection_id = null;
private $password = null;
private $doHTTPS = true;
private $requestMethod = 1;
protected $endpointHTTP = "http://www.smslink.ro/sms/gateway/communicate/index.php";
protected $endpointHTTPS = "https://secure.smslink.ro/sms/gateway/communicate/index.php";
public $communicationLogs = array();
/**
* Initialize SMSLink - SMS Gateway
*
* Initializing SMS Gateway will require the parameters $connection_id and $password. $connection_id and $password can be generated at
* https://www.smslink.ro/sms/gateway/setup.php after authenticated with your account credentials.
*
* @param string $connection_id SMSLink - SMS Gateway - Connection ID
* @param string $password SMSLink - SMS Gateway - Password
*
* @return void
*/
public function __construct($connection_id, $password)
{
if (!is_null($connection_id))
$this->connection_id = $connection_id;
if (!is_null($password))
$this->password = $password;
if ((is_null($this->connection_id)) or (is_null($this->password)))
exit("SMS Gateway initialization failed, credentials not provided. Please see documentation.");
}
public function __destruct()
{
$this->connection_id = null;
$this->password = null;
$this->doHTTPS = true;
$this->requestMethod = 1;
}
/**
* Sets the method in which the parameters are sent to SMS Gateway
*
* @param int $requestMethod 1 for cURL GET (recommended and default value)
* 2 for cURL POST
* 3 for file_get_contents (recommended if you do not have PHP cURL installed)
*
* @return bool true if method was set or false otherwise
*/
public function setRequestMethod($requestMethod = 1)
{
if (in_array($requestMethod, array(1, 2, 3))) $this->requestMethod = $requestMethod;
else return false;
return true;
}
/**
* Returns the method in which the parameters are sent to SMS Gateway
*
* @return int
*/
public function getRequestMethod()
{
return $this->requestMethod;
}
/**
* Sets the protocol that will be used by SMS Gateway (HTTPS or HTTP).
*
* @param string $methodName POST or GET
*
* @return bool true if method was set or false otherwise
*/
public function setProtocol($protocolName = "HTTPS")
{
$protocolName = strtoupper($protocolName);
if ($protocolName == "HTTPS") $this->doHTTPS = true;
elseif ($protocolName == "HTTP") $this->doHTTPS = false;
else return false;
return true;
}
/**
* Returns the protocol that is used by SMS Gateway (HTTPS or HTTP)
*
* @return string GET or POST possible values
*/
public function getProtocol()
{
return ($this->doHTTPS) ? "HTTPS" : "HTTP";
}
/**
* Sends SMS
*
* @param string $receiverNumber Receiver mobile phone number. Phone numbers should be formatted as a Romanian national mobile phone number (07xyzzzzzz)
* or as an International mobile phone number (00 + Country Code + Phone Number, example 0044zzzzzzzzz).
*
* @param string $messageText Message of the SMS, up to 160 alphanumeric characters, or longer than 160 characters.
*
* @param string $senderId (Optional) Sender alphanumeric string:
*
* numeric - sending will be done with a shortcode (ex. 18xy, 17xy)
* SMSLink.ro - sending will be done with SMSLink.ro (use this for tests only)
*
* Any other preapproved alphanumeric sender assigned to your account:
*
* Your alphanumeric sender list: http://www.smslink.ro/sms/sender-list.php
* Your alphanumeric sender application: http://www.smslink.ro/sms/sender-id.php
*
* Please Note:
*
* SMSLink.ro sender should be used only for testing and is not recommended to be used in production. Instead, you
* should use numeric sender or your alphanumeric sender, if you have an alphanumeric sender activated with us.
*
* If you set an alphanumeric sender for a mobile number that is in a network where the alphanumeric sender has not
* been activated, the system will override that setting with numeric sender.
*
*
* @param int $timestampProgrammed (Optional) Should be 0 (zero) for immediate sending or other UNIX timestamp in the future for future sending
*
* @return int representing SMSLink Message ID on success or false on failure.
*
*/
public function sendMessage($receiverNumber, $messageText, $senderId = NULL, $timestampProgrammed = 0)
{
$messageId = false;
$requestURL = ($this->getProtocol() == "HTTPS") ? $this->endpointHTTPS : $this->endpointHTTP;
$requestParameters = array(
"connection_id" => $this->connection_id,
"password" => $this->password,
"to" => $receiverNumber,
"message" => $messageText,
);
if (!is_null($senderId))
$requestParameters["sender"] = urlencode($senderId);
if (!is_null($timestampProgrammed))
if ($timestampProgrammed > 0)
$requestParameters["timestamp"] = $timestampProgrammed;
$requestResult = $this->sendRequest($requestURL, $requestParameters);
$requestResult = explode(";", $requestResult);
if ($requestResult[0] == "MESSAGE")
{
$requestResultVariabiles = explode(",", $requestResult[3]);
$messageId = $requestResultVariabiles[0];
}
return $messageId;
}
/**
* Account Balance
*
* @return array associative array decribing national-SMS and internationa-SMS account balance
*
*/
public function accountBalance()
{
$accountBalance = array(
"national-SMS" => 0,
"international-SMS" => 0
);
$requestURL = ($this->getProtocol() == "HTTPS") ? $this->endpointHTTPS : $this->endpointHTTP;
$requestParameters = array(
"connection_id" => $this->connection_id,
"password" => $this->password,
"mode" => "account-balance"
);
$requestResult = $this->sendRequest($requestURL, $requestParameters);
$requestResult = explode(";", $requestResult);
if ($requestResult[0] == "MESSAGE")
{
$requestResultVariabiles = explode(",", $requestResult[3]);
$accountBalance = array(
"national-SMS" => $requestResultVariabiles[0],
"international-SMS" => $requestResultVariabiles[1]
);
}
return $accountBalance;
}
/**
* Sends Request to SMSLink
*
* @param string $requestURL
* @param array $requestParameters
*
* @return string
*
*/
private function sendRequest($requestURL, $requestParameters)
{
$requestResult = false;
$returnedResult = "ERROR;0;Unknown error.";
$requestMethod = $this->getRequestMethod();
$logMessage = date("d-m-Y H:i:s")." - Sending Request using ";
if ($requestMethod == 1)
$logMessage = $logMessage."cURL GET";
if ($requestMethod == 2)
$logMessage = $logMessage."cURL POST";
if ($requestMethod == 3)
$logMessage = $logMessage."file_get_contents()";
$serializedParameters = http_build_query($requestParameters);
if (($requestMethod == 1) or ($requestMethod == 2))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestURL.(($requestMethod == 1) ? "?".$serializedParameters : ""));
$logMessage = $logMessage." to URL: [".$requestURL.(($requestMethod == 1) ? "?".$serializedParameters : "")."]";
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($requestMethod == 2)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $serializedParameters);
$logMessage = $logMessage." with POST parameters: [".$serializedParameters."]";
}
if (strpos($requestURL, "https://") !== false)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$requestResult = curl_exec($ch);
$connectionErrorCode = curl_errno($ch);
$connectionErrorMessage = curl_error($ch);
$requestStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($connectionErrorCode == 0)
{
if (($requestStatusCode >= 200) and ($requestStatusCode <= 299))
{
$returnedResult = $requestResult;
}
else
{
$returnedResult = "ERROR;0;Unexpected HTTP code ".$requestStatusCode;
}
}
else
{
$returnedResult = "ERROR;0;".$connectionErrorMessage;
}
curl_close($ch);
}
else
{
if ($requestMethod == 3)
{
$requestResult = file_get_contents($requestURL."?".$serializedParameters);
$logMessage = $logMessage." to URL: [".$requestURL."?".$serializedParameters."]";
if ($requestResult !== false)
{
$returnedResult = $requestResult;
}
else
{
$returnedResult = "ERROR;0;Connection failed using file_get_contents().";
}
}
}
$logMessage = $logMessage." => Request Result: [".$returnedResult."]";
$this->communicationLogs[] = $logMessage;
return $returnedResult;
}
/**
* Returns the latest log message from communication log
*
* @return string
*/
public function getLastLogMessage()
{
return $this->communicationLogs[sizeof($this->communicationLogs) - 1];
}
/**
* Displays the communication log
*
* @return string
*/
public function displayLogMessages()
{
echo "<b>Communication Log:</b><br />";
foreach ($this->communicationLogs as $key => $logMessage)
echo $logMessage."<br />";
}
}
/*
*
* Usage Examples for the SMSLinkSMSGateway() class
*
*
*/
/*
*
*
* Initialize SMS Gateway
*
* Get your SMSLink / SMS Gateway Connection ID and Password from
* https://www.smslink.ro/get-api-key/
*
*
*
*/
$SMSGateway = new SMSLinkSMSGateway("MyConnectionID", "MyConnectionPassword");
/*
* Sets the method in which the parameters are sent to SMS Gateway
*
* 1 for cURL GET (make sure you have PHP cURL installed) (default and recommended)
* 2 for cURL POST (make sure you have PHP cURL installed)
* 3 for file_get_contents (requires allow_url_fopen to be set to 1 in php.ini) (recommended if you do not have PHP cURL installed)
*/
$SMSGateway->setRequestMethod(1);
/*
* Sets the protocol that will be used by SMS Gateway (HTTPS or HTTP).
*/
$SMSGateway->setProtocol("HTTPS");
/*
* Display account balance before sending SMS
*/
$accountBalance = $SMSGateway->accountBalance();
echo "My Account Balance is: ".$accountBalance["national-SMS"]." national SMS, ".$accountBalance["international-SMS"]." national SMS<br />";
/*
* Sends SMS #1
*/
$messageId = $SMSGateway->sendMessage("07xyzzzzzz", "My first hello world message.");
if ($messageId == false) echo "Message sent failed. Log: ".$SMSGateway->getLastLogMessage().".<br />";
else echo "Message successfully sent with ID: ".$messageId.".<br />";
/*
* Sends SMS #2
*/
$messageId = $SMSGateway->sendMessage("07xyzzzzzz", "My second hello world message.");
if ($messageId == false) echo "Message sent failed. Log: ".$SMSGateway->getLastLogMessage().".<br />";
else echo "Message successfully sent with ID: ".$messageId.".<br />";
/*
* Display account balance after sending SMS
*/
$accountBalance = $SMSGateway->accountBalance();
echo "My Account Balance is: ".$accountBalance["national-SMS"]." national SMS, ".$accountBalance["international-SMS"]." national SMS<br />";
/*
* Display the communication log with SMSLink
*/
$SMSGateway->displayLogMessages();
?>