Displaying the Currently Playing Video in your Channel

Sample PHP Code

Create the file now_playing.php on your webserver, and use the following example code to query the API for viewers to your ScaleEngine application. Just add your API keys, and your CDN number.


<?php
$api_key 
"00000000000000000000000000000000"
$api_secret "0000000000000000000000000000000000000000000000000000000000000000"


$arrAPI = array(
    
'api_key' => $api_key,
    
'cdn' => 500,
    
'command' => 'channel.nowplaying',
        
'app' => 'steftest-channel',
        
'stream' => 'channel4',
    
'timestamp' => time(),
    );

$request json_encode($arrAPI);
$sig base64_encode(hash_hmac('sha256'$request$api_secrettrue));
$arrAPI['signature'] = $sig;
$request json_encode($arrAPI);

$ch curl_init();
curl_setopt($chCURLOPT_URL"https://api.scaleengine.net/stable/"); // Set the URL
curl_setopt($chCURLOPT_POSTtrue); // Perform a POST
curl_setopt($chCURLOPT_RETURNTRANSFERtrue); // If not set, curl prints output to the browser
curl_setopt($chCURLOPT_HEADERfalse); // If set, curl returns headers as part of the data stream
curl_setopt($chCURLOPT_POSTFIELDS, array("json" => $request));

curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse); // Turns off verification of the SSL certificate.
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse); // Turns off verification of the SSL certificate.

$response curl_exec($ch); //Execute the API Call
if (!$response) {
  die(
'Failed to connect to ScaleEngine API');
}
//echo $response;

$arrResponse=array();
$videos=array();
$arrResponse json_decode($responsetrue);

if (
$arrResponse) {
  if (
$arrResponse['status'] == 'success') {
        
$videos=$arrResponse['videos'];
        
//the 'video' is the currently playing source, you probably want to edit this down to the file name
        //we plan to add a description field so you can show that
        //to see everything the API returns, do a var_dump       
        //var_dump($videos);
        //we return 'previous' 'current' and 'next' - here, we show current        
        
$current $videos['current'];        
        echo 
preg_replace("|sestore1/username/|"""$current);
      } else {
    die(
"Error: API call failed. {$arrResponse['message']}");
  }
} else {
  die(
'Unknown API Error');
}

?>


Sample JQuery

Create the file now_playing.js on your webserver, and use the following example code to query the API every 60 seconds by calling the php script above.


        function now_playing() {
            jQuery.ajax({
                url: '/now_playing.php',
                dataType: 'html',
                type: 'get',
                success: function(data) {
                    jQuery('#now_playing').html(data).show;
                    setTimeout( function(){ now_playing(); } ,60000);
                }
            });
        }
function jqueryLoaded() {
        jQuery(document).ready(function() {
                now_playing();
        });
}

function checkJquery() {
    if (window.jQuery) {
        jqueryLoaded();
    } else {
        window.setTimeout(checkJquery, 100);
    }
}

checkJquery();

Sample HTML

Finally, add a small div with span id that matches the ajax js function name (now_playing) to display the returned number from the API on your website and have it check in for updates every 60 seconds.


<div style="display: inline; width: 25px; color: #5588BB;"><span id="now_playing" style="text-align: center;"></span></div>
<script type="text/javascript" src="/now_playing.js"></script>