Suggestions

close search

Add Messaging, Voice, and Authentication to your apps with Vonage Communications APIs

Visit the Vonage API Developer Portal
Developer Documentation moved on August 7, 2026. Future updates are available only at → https://developer.vonage.com/en/opentok/overview

Connection Token Creation — PHP

In order to authenticate a user connecting to an OpenTok session, a client must connect using a token (see this overview).

The following PHP code example shows how to generate a token using the OpenTok PHP server-side library:

<?PHP

use OpenTok\OpenTok;

$opentok = new OpenTok(API_KEY,API_SECRET);
// Replace with the correct session ID:
print $opentok->generateToken('your_session_ID');
print "\n";
?>

Calling the generateToken() method returns a string. This string is the token. Use server-side PHP code to include the token in the served web page.

The following PHP code example shows how to obtain a token that has a role of "publisher" and that has a connection metadata string:

<?PHP

use OpenTok\OpenTok;

$opentok = new OpenTok(API_KEY,API_SECRET);
// Replace with meaningful metadata for the connection:
$connectionMetaData = "username=Bob,userLevel=4";
// Replace with the correct session ID:
print $opentok->generateToken('your_session_ID', array('role' => Role::PUBLISHER, 'expireTime' => time()+(7 * 24 * 60 * 60), 'data' =>  $connectionMetaData);
print "\n";
?>

The method takes the following arguments: