-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsendpulse.php
78 lines (70 loc) · 2.22 KB
/
sendpulse.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
<?php
/**
* SendPulse SMS Gateway
* @autor RenatoAscencio
*/
define("SENDPULSE_GATEWAY", [
"apiUrl" => "https://api.sendpulse.com",
"apiId" => "YOUR_API_ID", // Replace with your SendPulse API ID
"apiSecret" => "YOUR_API_SECRET", // Replace with your SendPulse API Secret
"senderName" => "YOUR_SENDER_NAME" // Replace with your configured sender name
]);
function getAccessToken() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SENDPULSE_GATEWAY["apiUrl"] . "/oauth/access_token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
"grant_type" => "client_credentials",
"client_id" => SENDPULSE_GATEWAY["apiId"],
"client_secret" => SENDPULSE_GATEWAY["apiSecret"]
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
return null;
}
curl_close($ch);
$response = json_decode($result, true);
return $response['access_token'] ?? null;
}
return [
"send" => function ($phone, $message, &$system) {
/**
* Implement sending here
* @return bool:true
* @return bool:false
*/
$accessToken = getAccessToken();
if (!$accessToken) {
return false;
}
$send = $system->guzzle->post(SENDPULSE_GATEWAY["apiUrl"] . "/sms/send", [
"headers" => [
"Authorization" => "Bearer " . $accessToken,
"Content-Type" => "application/json"
],
"json" => [
"sender" => SENDPULSE_GATEWAY["senderName"],
"phones" => [$phone],
"body" => $message
],
"allow_redirects" => true,
"http_errors" => false
]);
if ($send->getStatusCode() == 200) {
return true;
} else {
return false;
}
},
"callback" => function ($request, &$system) {
/**
* Implement status callback here if gateway supports it
* @return array:MessageID
* @return array:Empty
*/
}
];
?>