-
Notifications
You must be signed in to change notification settings - Fork 1
/
sms.php
77 lines (67 loc) · 1.91 KB
/
sms.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
<?php
/**
* @author Simaranjit Singh <simaranjit.singh@virdi.me>
*/
class sms
{
// Plivo AUTH ID
private $AUTH_ID;
// Plivo AUTH TOKEN
private $AUTH_TOKEN;
// Plivo contact number
private $PHONE_NUMBER;
/**
* @param string $authID
* @param string $authToken
* @param string $phoneNumber
*/
public function __construct($authID, $authToken, $phoneNumber)
{
// Configure keys here
$this->AUTH_ID = $authID;
$this->AUTH_TOKEN = $authToken;
$this->PHONE_NUMBER = $phoneNumber;
}
/**
* @param array $params
*
* @return string
*/
private function curl($params)
{
$ch = curl_init('https://api.plivo.com/v1/Account/' . $this->AUTH_ID . '/Message/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->AUTH_ID . ":" . $this->AUTH_TOKEN);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* @param string $destination
* @param string $text
*
* @return array
*
* @throws Exception
*/
public function send($destination, $text)
{
if (trim($destination) == '') {
throw new Exception('EMPTY_DESTINATION_NUMBER');
} else if (trim($text) == '') {
throw new Exception('EMPTY_MESSAGE_CONTENT');
}
$params = array(
"src" => $this->PHONE_NUMBER,
"dst" => $destination,
"text" => $text
);
return json_decode($this->curl($params), true);
}
}