PHP code example of peterujah / php-agora-tokens

1. Go to this page and download the library: Download peterujah/php-agora-tokens library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

peterujah / php-agora-tokens example snippets


use Peterujah\Agora\Agora;
use Peterujah\Agora\User;
use Peterujah\Agora\Roles;
use Peterujah\Agora\Builders\RtcToken;

// Example inputs
$channelName = "7d72365eb983485397e3e3f9d460bdda";
$uid = 2882341273;
$uidStr = "2882341273";
$maxTokenLive = 3600;

// Generate privilege expiration timestamp (UTC)
$currentTimestamp = (new DateTime("now", new DateTimeZone('UTC')))->getTimestamp();
$privilegeExpiredTs = $currentTimestamp + $maxTokenLive;

// Initialize Agora client using environment variables
$client = new Agora(
  getenv("AGORA_APP_ID"),           // Set this in your environment
  getenv("AGORA_APP_CERTIFICATE")   // Set this in your environment
);
$client->setExpiration($privilegeExpiredTs); // Agora Token expiration

// User using UID (int)
$user1 = (new User($uid))
  ->setPrivilegeExpire($privilegeExpiredTs)
  ->setChannel($channelName)
  ->setRole(Roles::RTC_PUBLISHER);

// Generate RTC token using legacy builder (AccessToken v007)
$token1 = RtcToken::buildTokenWithUid($client, $user1);
echo 'Token with int UID: ' . $token1 . PHP_EOL;

// User using User Account (string)
$user2 = (new User($uidStr))
  ->setPrivilegeExpire($privilegeExpiredTs)
  ->setChannel($channelName)
  ->setRole(Roles::RTC_PUBLISHER);

// Generate RTC token using legacy builder with user account
$token2 = RtcToken::buildTokenWithUserAccount($client, $user2);
echo 'Token with user account: ' . $token2 . PHP_EOL;