Reporting Live Status of Livestreams with PHP & jQuery
DEMO
References:
- Livestream Channel API 2.0
- Zend_Json
- jQuery Ajax
- Livestream
islive.php
<?php
/*
ini_set('display_errors', 1);
error_reporting(E_ALL);
*/
/*
* http://framework.zend.com/manual/en/zend.json.html
*/
include_once 'Json.php';
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
try {
$url = '';
switch ($_GET['name']) {
case 'cowclan':
$url = 'http://xcowclanx.channel-api.livestream-api.com/2.0/livestatus';
break;
case 'rebootgpf':
$url = 'http://xrebootgpfx.channel-api.livestream-api.com/2.0/livestatus';
break;
case 'clgaming':
$url = 'http://xclgamex.channel-api.livestream-api.com/2.0/livestatus';
break;
}
function isLive($url) {
$data = file_get_contents($url);
$xml = new SimpleXMLElement($data);
$nsdata = $xml->children('http://api.channel.livestream.com/2.0');
$list = array();
foreach($nsdata as $c) {
$list[$c->getName()] = (string)$c;
}
return $_GET['callback'].'('.Zend_Json::encode($list).')';
}
if (!empty($url)) {
echo isLive($url);
} else {
echo $_GET['callback'].'('.Zend_Json::encode(array('error'=>'404')).')';
}
} catch (Exception $e) {
echo $_GET['callback'].'('.Zend_Json::encode(array('error'=>'503')).')';
}
$err = error_get_last();
if (!empty($err)) {
echo $_GET['callback'].'('.Zend_Json::encode(array('error'=>'500')).')';
}
?>
Code Snippet from the DEMO
function updateStreamInfo(channel, element, name) {
$.ajax({
url : "http://ingol.nl/lol/livestream/islive.php?name="+channel,
type : "GET",
dataType : "jsonp",
success : function(data, textStatus, XMLHttpRequest){
var color = {'color':'#FFFFFF'};
if (data.isLive && data.isLive == 'true') {
color = {'color':'#FF0000'};
} else {
color = {'color':'#FFFFFF'};
}
var viewercount = 'undefined' === typeof(data.currentViewerCount) ? '-.-' : data.currentViewerCount;
var link = $('<a/>').attr({'href':'http://livestream.com/'+channel,"target":"livestream"}).css(color)
.html('LIVESTREAM '+name.toUpperCase()+' ('+viewercount+')');
element.html(link);
},
error : function(XMLHttpRequest, textStatus, errorThrown){
// console.debug(XMLHttpRequest);
}
});
}
function isLiveStatus(name,channel) {
var minutes = 5;
updateStreamInfo(channel,$('#'+name), name);
window.setInterval('updateStreamInfo("'+channel+'",$("#'+name+'"), "'+name+'")', 1000 * 60 * minutes);
}
$(function(){
isLiveStatus('rebootgpf','rebootgpf');
isLiveStatus('clgaming','clgame');
isLiveStatus('cowclan','cowclan');
isLiveStatus('Team-AON','teamaon');
$('#popup').click(function(){
window.open('index.html','livestreams','width=400,height=200,menubar=no,status=no,location=no,toolbar=no,scrollbars=no');
});
});
Am I blind, or how are you calling currentViewerCount from Livestream? This way will only display 0 or your undefined. (-.-)