Create basic backend tutorial for PHP
This commit is contained in:
parent
8e791617b5
commit
e74ddedd3b
2
basic/backend/php/.env
Normal file
2
basic/backend/php/.env
Normal file
@ -0,0 +1,2 @@
|
||||
LIVEKIT_API_KEY=devkey
|
||||
LIVEKIT_API_SECRET=secret
|
||||
6
basic/backend/php/.gitignore
vendored
Normal file
6
basic/backend/php/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
composer.phar
|
||||
/vendor/
|
||||
|
||||
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
|
||||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
|
||||
# composer.lock
|
||||
22
basic/backend/php/composer.json
Normal file
22
basic/backend/php/composer.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "openvidu/basic-php",
|
||||
"description": "Basic server application built for PHP",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"vlucas/phpdotenv": "^5.6",
|
||||
"agence104/livekit-server-sdk": "^1.2"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "openvidu"
|
||||
}
|
||||
],
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"php-http/discovery": true
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"start": "php -S localhost:6080"
|
||||
}
|
||||
}
|
||||
1436
basic/backend/php/composer.lock
generated
Normal file
1436
basic/backend/php/composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
47
basic/backend/php/index.php
Normal file
47
basic/backend/php/index.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use Agence104\LiveKit\AccessToken;
|
||||
use Agence104\LiveKit\AccessTokenOptions;
|
||||
use Agence104\LiveKit\VideoGrant;
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
Dotenv::createImmutable(__DIR__)->safeLoad();
|
||||
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
$LIVEKIT_API_KEY = $_ENV['LIVEKIT_API_KEY'] ?? 'devkey';
|
||||
$LIVEKIT_API_SECRET = $_ENV['LIVEKIT_API_SECRET'] ?? 'secret';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['PATH_INFO'] === '/token') {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$roomName = $data['roomName'] ?? null;
|
||||
$participantName = $data['participantName'] ?? null;
|
||||
|
||||
if (!$roomName || !$participantName) {
|
||||
http_response_code(400);
|
||||
echo "roomName and participantName are required";
|
||||
exit();
|
||||
}
|
||||
|
||||
$tokenOptions = (new AccessTokenOptions())
|
||||
->setIdentity($participantName);
|
||||
$videoGrant = (new VideoGrant())
|
||||
->setRoomJoin()
|
||||
->setRoomName($roomName);
|
||||
$token = (new AccessToken($LIVEKIT_API_KEY, $LIVEKIT_API_SECRET))
|
||||
->init($tokenOptions)
|
||||
->setGrant($videoGrant)
|
||||
->toJwt();
|
||||
|
||||
echo $token;
|
||||
exit();
|
||||
}
|
||||
|
||||
echo "Unsupported endpoint or method";
|
||||
exit();
|
||||
Loading…
x
Reference in New Issue
Block a user