Create basic backend tutorial for PHP

This commit is contained in:
juancarmore 2024-04-27 14:46:50 +02:00
parent 8e791617b5
commit e74ddedd3b
5 changed files with 1513 additions and 0 deletions

2
basic/backend/php/.env Normal file
View File

@ -0,0 +1,2 @@
LIVEKIT_API_KEY=devkey
LIVEKIT_API_SECRET=secret

6
basic/backend/php/.gitignore vendored Normal file
View 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

View 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

File diff suppressed because it is too large Load Diff

View 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();