-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_token.php
More file actions
66 lines (55 loc) · 1.9 KB
/
generate_token.php
File metadata and controls
66 lines (55 loc) · 1.9 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
<?php
/**
* @wp-staging
* Site : https://keiste.com/staging
* Parent : https://keiste.com
* Created at : 31.07.2025 12:50:35
* Updated at : 31.07.2025 12:50:35
* Read more : https://wp-staging.com/docs/create-a-staging-site-clone-wordpress/
*/
// File: generate_token.php
// Place this file in your plugin directory or a public-accessible location
header('Content-Type: application/json');
define('WP_CACHE', false);
require_once __DIR__ . '/vendor/autoload.php'; // Adjust path if needed
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VoiceGrant;
// Read credentials from key.txt
function readKeyValue($keyName) {
$keyFile = __DIR__ . '/key.txt';
if (!file_exists($keyFile)) {
return null;
}
$lines = file($keyFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos($line, $keyName . '=') === 0) {
return trim(substr($line, strlen($keyName) + 1));
}
}
return null;
}
$twilioAccountSid = readKeyValue('TWILIO_ACCOUNT_SID');
$twilioApiKey = readKeyValue('TWILIO_API_KEY');
$twilioApiSecret = readKeyValue('TWILIO_API_KEY_SECRET');
$outgoingApplicationSid = readKeyValue('TWILIO_TWIML_APP_SID');
// The identity for this user (alphanumeric, dashes, underscores only)
$identity = isset($_GET['identity']) ? preg_replace('/[^a-zA-Z0-9_\-]/', '', $_GET['identity']) : 'User';
if (!$twilioAccountSid || !$twilioApiKey || !$twilioApiSecret || !$outgoingApplicationSid) {
http_response_code(500);
echo 'Twilio credentials missing.';
exit;
}
$token = new AccessToken(
$twilioAccountSid,
$twilioApiKey,
$twilioApiSecret,
17200,
"User",
"ie1"
);
// Create Voice grant
$voiceGrant = new VoiceGrant();
$voiceGrant->setOutgoingApplicationSid($outgoingApplicationSid);
$voiceGrant->setIncomingAllow(true);
$token->addGrant($voiceGrant);
echo json_encode(['token' => $token->toJWT()]);