-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelstra_sms.php
More file actions
80 lines (64 loc) · 2.69 KB
/
Copy pathtelstra_sms.php
File metadata and controls
80 lines (64 loc) · 2.69 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
<?php
class TelstraSMS
{
private $appKey = "", $appSecret = "", $recipient = "", $message = "", $accessToken = "", $messageId = "";
function __construct($appKey, $appSecret, $recipient, $message)
{
$this->appKey = $appKey;
$this->appSecret = $appSecret;
$this->recipient = $recipient;
$this->message = $message;
$this->recipient = str_replace(" ", "", $recipient); // Remove space characters in phone number if present
}
public function send()
{
$this->accessToken = $this->authenticate(); // Get auth token
$this->messageId = $this->sendMessage(); // Send the message
}
private function authenticate()
{
$base = "https://api.telstra.com/v1/oauth/token?client_id=" . $this->appKey . "&client_secret=" .
$this->appSecret . "&grant_type=client_credentials&scope=SMS";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, "Telstra SMS");
//curl_setopt($curl, CONNECTTIMEOUT, 1);
$content = curl_exec($curl);
curl_close($curl);
return json_decode($content, true)["access_token"];
}
private function sendMessage()
{
$base = "https://api.telstra.com/v1/sms/messages";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, "Telstra SMS");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(("Authorization: Bearer " . $this->accessToken)));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array(
"to" => $this->recipient,
"body" => $this->message
)));
//curl_setopt($curl, CONNECTTIMEOUT, 1);
$content = curl_exec($curl);
curl_close($curl);
return json_decode($content, true)["messageId"];
}
public function getStatus()
{
$base = "https://api.telstra.com/v1/sms/messages/" . $this->messageId;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, "Telstra SMS");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(("Authorization: Bearer " . $this->accessToken)));
//curl_setopt($curl, CONNECTTIMEOUT, 1);
$content = curl_exec($curl);
curl_close($curl);
return json_decode($content, true);
}
}