-
Notifications
You must be signed in to change notification settings - Fork 66
/
send.php
74 lines (56 loc) · 2.28 KB
/
send.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
<?php
include 'core/init.php';
include 'core/config.php';
header('Content-type: application/json');
// Insert Twillio AccountSid and AuthToken from www.twilio.com/user/account
// into core/config.php
if (isset($_POST['sendSMS']) && !empty($_POST['sendSMS'])){
$required_fields = [
"sender",
"recipient",
"message"
];
//Itterate through POST data and check it against the
//required fields to ensure that all data is at hand
foreach ($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true){
$responseArray['success'] = false;
$responseArray['status'] = "error";
$responseArray['message'] = "All fields must be filled";
break 1;
}
}
if(!isset($config['AccountSid']) || !isset($config['AuthToken'])){
$responseArray['success'] = false;
$responseArray['status'] = "error";
$responseArray['message'] = "You must fill in your Account info. See core/config.php for more information.";
}
if (!isset($responseArray['success'])){
//Add POST data to array
$data = [
"sender" => trim($_POST['sender']),
"recipient" => "+".str_replace(' ', '' ,trim($_POST['recipient'])),
"message" => trim($_POST['message'])
];
// Creates new Twilio rest client
$client = new Services_Twilio($config['AccountSid'], $config['AuthToken']);
// Send message to number
$sms = $client->account->messages->sendMessage(
// This is the 'Sender'
$data['sender'],
// The number the message is being sent to
$data['recipient'],
// The sms body
$data['message']
);
// Return response
$responseArray = [
"success" => true,
"status" => "success",
"message" => "Sent message to " . $data['recipient'] . ".",
"data" => $data
];
}
echo json_encode($responseArray);
}
?>