Suggestions

close search

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

Visit the Vonage API Developer Portal

Session Creation — PHP

You can use the OpenTok PHP library to generate OpenTok sessions. Each session has a unique session ID, which you can use in an OpenTok client library to connect to an OpenTok session. (See "Joining a Session" for Web, iOS, or Android.)

Creating a session that uses the OpenTok Media Router

The following code session that uses the OpenTok Media Router:

<?php
    use OpenTok\OpenTok;

    $apiObj = new OpenTok($API_KEY, $API_SECRET);
    $session = $apiObj->createSession(array('mediaMode' => MediaMode::ROUTED));
    echo $session->getSessionId();
?>

Use the session ID in an OpenTok client library to connect to an OpenTok session.

In an HTML page that uses the OpenTok.js library, you can embed PHP that references the API key, the session ID, and the generated token string, as in the following:

var apiKey = <?php print $API_KEY?>;
var sessionId = '<?php print $sessionId; ?>';
var token = '<?php print $apiObj->generateToken($sessionId); ?>';

You will also need to generate a token for each user connecting to the OpenTok session. See Connection Token Creation for information on the generateToken() method.

The OpenTok Media Router provides the following benefits:

Creating a relayed session

Here is PHP sample code that creates a new session with the media mode set to relayed:

<?php
use OpenTok\OpenTok;

$apiObj = new OpenTok($API_KEY, $API_SECRET);

$session = $apiObj->createSession();
echo $session->getSessionId();
?>

In a relayed session, clients will attempt to send streams directly between each other. However, if clients cannot connect due to firewall restrictions, the session uses the OpenTok TURN server to relay audio-video streams.

Important: Some features, such as archiving, are only available in routed (not relayed) sessions. See the previous section for instructions on creating a routed session.

Creating an automatically archived session

You can create a session that is automatically archived. Here is PHP sample code that creates an automatically archived session:

$sessionOptions = array(
    'archiveMode' => ArchiveMode::ALWAYS,
    'mediaMode' => MediaMode::ROUTED
);
$session = $opentok->createSession($sessionOptions);

echo $session->getSessionId();

Note that archived sessions must use the routed media mode.

For more information, see the archiving developer guide.

Using sessions in client applications

Use the session ID in an OpenTok client SDK to connect to an OpenTok session.

You will also need to generate a token for each user connecting to the OpenTok session. See Connection Token Creation for information on the generateToken() method.