Search code examples
phpopentok

How to Show Error Messages from OpenTok/PHP


I'm trying to use the OpenTok PHP SDK but can't seem to get it to work from the start. The code below will work and return the "Success" message. However, if I try to create a new OpenTok object by commenting that line, the message will no longer display.

Is there a way to get some error reporting on what's going on here or other things I should verify?

<?php

   require_once 'OpenTok/OpenTok.php';
   require_once 'OpenTok/Session.php';

   //$apiObj = new OpenTok("*******", "********");

   echo "Success";

?>

Solution

  • In general, you should first understand how PHP does exception handling

    In this case, one quick way to get an error message would be to wrap the code in a try/catch block

    try {
        require_once 'OpenTok/OpenTok.php';
        require_once 'OpenTok/Session.php';
    
        $apiObj = new OpenTok("*******", "********");
    
        echo "Success";
    } catch (Exception $e) {
        echo "Caught exception: ",  $e->getMessage(), "\n";
    }
    

    More specifically, if you are using the latest OpenTok PHP SDK, the recommended way to load the classes is using a PSR-0 autoloader, such as the one generated by Composer.