Work with OpenTok in Composer.JSON

1

I have a PHP project and its structure is as follows:

The main folder is called OpenTok, inside it there are two files, the index.php and the composer.json and another folder also called OpenTok that includes all the files in the package. The problem is that when I try to generate a session in the following way, it sends me an error:

    <?php
    use OpenTok\OpenTok;

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

The error you send me is the following:

  

Fatal error: Class 'OpenTok \ OpenTok' not found in   C: \ xampp \ htdocs \ OpenTok \ index.php on line 4

I hope and can help me find a solution. Greetings.

Adding more information, the composer.json I have is the following:

    {
    "name": "Opentok/Opentok",
    "description": "OpenTok is a platform for creating real time streaming video applications, created by TokBox.",
    "type": "library",
    "keywords": [
        "TokBox",
        "OpenTok",
        "PHP",
        "WebRTC",
        "video",
        "streaming"
    ],
    "homepage": "https://github.com/opentok/Opentok-PHP-SDK",
    "license": "MIT",
    "authors": [
        {
            "name": "Ankur Oberoi",
            "email": "[email protected]",
            "role": "Developer"
        },
        {
            "name": "Community contributors",
            "homepage": "https://github.com/opentok/Opentok-PHP-SDK/graphs/contributors"
        }
    ],
    "support": {
        "email": "[email protected]",
        "issues": "https://github.com/opentok/Opentok-PHP-SDK/issues"
    },
    "require": {
        "php": ">=5.0.0",
        "guzzle/guzzle": "~3.7",
        "aoberoi/json-works": "~1.0",
        "firebase/php-jwt": "^3.0"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.1",
        "phing/phing": "dev-master"
    },
    "autoload": {
        "psr-4": {
            "OpenTok\": "src/OpenTok"
        }
    }
}
    
asked by Guillermo Ricardo Spindola Bri 14.11.2016 в 18:15
source

1 answer

1

If you are not using a Framework or something like that you will have to include in the first line the require 'OpenTok/autoload.php or include OpenTok/autoload.php or generally the file where the class you want to instantiate / call is located:

<?php
require 'OpenTok/vendor/autoload.php';

use OpenTok\OpenTok;

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

According to the OpenTok documentation the autoload.php file is located in the vendor directory:

  

This package follows the PSR-4 autoloading standard. If you are using composer to install, you just require the generated autoloader:    require "/vendor/autoload.php" ;

If you are using a framework it is normal to use a autoload.php file to avoid the first.

Edit:

Step 0:

If you use Composer you will have a composer.json file in your home directory, otherwise, you must download it from here: link

Step 1:

Automatically add the OpenTok package from your command line (example for Linux):

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

Or add in your file composer.json within require:

"require": {
    ...
    "opentok/opentok": "2.3.x"
    ...
}

Step 2:

If you do not use Composer it will not import the file composer.json . Then you can download the OpenTok package from here and reference it in the PHP file you are going to use in the form what I told you earlier:

<?php
require 'OpenTok/vendor/autoload.php';

use OpenTok\OpenTok;

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

Tell us how you are going there. Good luck.

    
answered by 14.11.2016 / 18:40
source