Giter Site home page Giter Site logo

inaniyants / opentok-php-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from opentok/opentok-php-sdk

0.0 1.0 1.0 1.21 MB

OpenTok PHP Server SDK

Home Page: https://tokbox.com/opentok/libraries/server/php/

License: MIT License

Shell 0.43% Batchfile 0.56% HTML 5.33% CSS 10.54% PHP 81.34% JavaScript 1.79%

opentok-php-sdk's Introduction

OpenTok PHP SDK

Build Status

The OpenTok PHP SDK lets you generate sessions and tokens for OpenTok applications, and archive sessions.

Installation

Composer (recommended):

Composer helps manage dependencies for PHP projects. Find more info here: http://getcomposer.org

Add this package (opentok/opentok) to your composer.json file, or just run the following at the command line:

$ ./composer.phar require opentok/opentok 2.4.x

Manually:

Download the opentok.phar file for the latest release from the Releases page.

Place opentok.phar in the include_path OR require it in any script which uses the OpenTok\* classes.

Usage

Initializing

This package follows the PSR-4 autoloading standard. If you are using composer to install, you just require the generated autoloader:

require "<projectpath>/vendor/autoload.php";

Once the files of the SDK are loaded, you initialize an OpenTok\OpenTok object with your own API Key and API Secret.

use OpenTok\OpenTok;

$opentok = new OpenTok($apiKey, $apiSecret);

Creating Sessions

To create an OpenTok Session, use the createSession($options) method of the OpenTok\OpenTok class. The $options parameter is an optional array used to specify the following:

  • Setting whether the session will use the OpenTok Media Router or attempt to send streams directly between clients.

  • Setting whether the session will automatically create archives (implies use of routed session)

  • Specifying a location hint.

The getSessionId() method of the OpenTok\Session instance returns the session ID, which you use to identify the session in the OpenTok client libraries.

use OpenTok\MediaMode;
use OpenTok\ArchiveMode;

// Create a session that attempts to use peer-to-peer streaming:
$session = $opentok->createSession();

// A session that uses the OpenTok Media Router, which is required for archiving:
$session = $opentok->createSession(array( 'mediaMode' => MediaMode::ROUTED ));

// A session with a location hint:
$session = $opentok->createSession(array( 'location' => '12.34.56.78' ));

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


// Store this sessionId in the database for later use
$sessionId = $session->getSessionId();

Generating Tokens

Once a Session is created, you can start generating Tokens for clients to use when connecting to it. You can generate a token either by calling the generateToken($sessionId, $options) method of the OpenTok\OpenTok class, or by calling the generateToken($options) method on the OpenTok\Session instance after creating it. The $options parameter is an optional array used to set the role, expire time, and connection data of the Token.

use OpenTok\Session;
use OpenTok\Role;

// Generate a Token from just a sessionId (fetched from a database)
$token = $opentok->generateToken($sessionId);
// Generate a Token by calling the method on the Session (returned from createSession)
$token = $session->generateToken();

// Set some options in a token
$token = $session->generateToken(array(
    'role'       => Role::MODERATOR,
    'expireTime' => time()+(7 * 24 * 60 * 60), // in one week
    'data'       => 'name=Johnny'
));

Working with Archives

You can only archive sessions that use the OpenTok Media Router (sessions with the media mode set to routed).

You can start the recording of an OpenTok Session using the startArchive($sessionId, $name) method of the OpenTok\OpenTok class. This will return an OpenTok\Archive instance. The parameter $archiveOptions is an optional array and is used to assign a name, whether to record audio and/or video, and the desired output mode for the Archive. Note that you can only start an Archive on a Session that has clients connected.

// Create a simple archive of a session
$archive = $opentok->startArchive($sessionId);


// Create an archive using custom options
$archiveOptions = array(
    'name' => 'Important Presentation',     // default: null
    'hasAudio' => true,                     // default: true
    'hasVideo' => true,                     // default: true
    'outputMode' => OutputMode::INDIVIDUAL  // default: OutputMode::COMPOSED
);
$archive = $opentok->startArchive($sessionId, $archiveOptions);

// Store this archiveId in the database for later use
$archiveId = $archive->id;

Setting the output mode to OutputMode::INDIVIDUAL setting causes each stream in the archive to be recorded to its own individual file. The OutputMode::COMPOSED setting (the default) causes all streams in the archive to be recorded to a single (composed) file.

You can stop the recording of a started Archive using the stopArchive($archiveId) method of the OpenTok\OpenTok object. You can also do this using the stop() method of the OpenTok\Archive instance.

// Stop an Archive from an archiveId (fetched from database)
$opentok->stopArchive($archiveId);
// Stop an Archive from an Archive instance (returned from startArchive)
$archive->stop();

To get an OpenTok\Archive instance (and all the information about it) from an archive ID, use the getArchvie($archiveId) method of the OpenTok\OpenTok class.

$archive = $opentok->getArchive($archiveId);

To delete an Archive, you can call the deleteArchive($archiveId) method of the OpenTok\OpenTok class or the delete() method of an OpenTok\Archive instance.

// Delete an Archive from an archiveId (fetched from database)
$opentok->deleteArchive($archiveId);
// Delete an Archive from an Archive instance (returned from startArchive, getArchive)
$archive->delete();

You can also get a list of all the Archives you've created (up to 1000) with your API Key. This is done using the listArchives($offset, $count) method of the OpenTok/OpenTok class. The parameters $offset and $count are optional and can help you paginate through the results. This will return an instance of the OpenTok\ArchiveList class.

$archiveList = $opentok->listArchives();

// Get an array of OpenTok\Archive instances
$archives = $archiveList->getItems();
// Get the total number of Archives for this API Key
$totalCount = $archiveList->totalCount();

Note that you can also create an automatically archived session, by passing in ArchiveMode::ALWAYS as the archiveMode key of the options parameter passed into the OpenTok->createSession() method (see "Creating Sessions," above).

For more information on archiving, see the OpenTok archiving programming guide.

Samples

There are two sample applications included in this repository. To get going as fast as possible, clone the whole repository and follow the Walkthroughs:

Documentation

Reference documentation is available at https://tokbox.com/developer/sdks/php/reference/index.html.

Requirements

You need an OpenTok API key and API secret, which you can obtain by logging into your TokBox account.

The OpenTok PHP SDK requires PHP 5.3 or greater.

Release Notes

See the Releases page for details about each release.

Important changes since v2.2.0

Changes in v2.2.1:

The default setting for the createSession() method is to create a session with the media mode set to relayed. In previous versions of the SDK, the default setting was to use the OpenTok Media Router (media mode set to routed). In a relayed session, clients will attempt to send streams directly between each other (peer-to-peer); if clients cannot connect due to firewall restrictions, the session uses the OpenTok TURN server to relay audio-video streams.

Changes in v2.2.0:

This version of the SDK includes support for working with OpenTok archives.

The names of many methods of the API have changed. Many method names have changed to use camel case, including the following:

  • \OpenTok\OpenTok->createSession()
  • \OpenTok\OpenTok->generateToken()

Note also that the options parameter of the OpenTok->createSession() method has a mediaMode property instead of a p2p property.

The API_Config class has been removed. Store your OpenTok API key and API secret in code outside of the SDK files.

See the reference documentation http://www.tokbox.com/opentok/libraries/server/php/reference/index.html and in the docs directory of the SDK.

Development and Contributing

Interested in contributing? We ❤️ pull requests! See the Development and Contribution guidelines.

Support

See https://support.tokbox.com for all our support options.

Find a bug? File it on the Issues page. Hint: test cases are really helpful!

opentok-php-sdk's People

Contributors

aoberoi avatar digitaltsai avatar djoos avatar eauge avatar gibbster avatar hbaqai avatar javer avatar jeffswartz avatar lrocher avatar manasdpradhan avatar mkascel avatar phoenixcw avatar pincombe avatar rskuipers avatar shivamthapar avatar smartmohi avatar songz avatar thepatrick avatar

Watchers

 avatar

Forkers

mstraa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.