i'm building a webpage for livestreamers from my website.
but i'm having some problems with it, I have created a table, and i have been screwing around with it a lot and i have had good results, but it isn't perfect yet, so i'm still stuck
current example http://brokendiamond.org/?q=node/9 ;
i got 2 problems with this; 1 my page loads in about 2 - 3 seconds wich is still a bit too long 2 the data in my table isn't updating like it is supposed too, when my stream goes live, it doesn't show in my table, even tho the xml file does get updated, and it doesn't show the view count correctly, so there is something wrong with that,
Any input about this, or help with this matter would be appreciated, because i realy want to get this part finished, so i can move on on my todo list.
I have heard peoples talk about cashes but i have no clue how to do that so that might be a direction.
the XML API can be found on: http://api.own3d.tv/liveCheck.php?live_id=210230
and the current code of my webpage is:
Hello and welcome to the Livestream page for the broken diamond community here you can find all our content creator's livestream pages to watch them game. you can chat with other members and even them! We always welcome new followers and will love to hear about suggestions for games, in game tips and all those opinions we know you have to share! Enjoy your favorite streamers, and don't forget their schedule can be found under the information block in the menu.
<div id="livetable">
</div>
<script type="text/javascript">
load();
var intervalID;
refresh();
function refresh()
{
intervalID = setInterval(load, 60000);
}
function load()
{
var elem = document.getElementById("livetable");
elem.innerHTML = '<?php loadpage() ?><br>';
}
</script>
<?php
define('ELEMENT_CONTENT_ONLY', true);
define('ELEMENT_PRESERVE_TAGS', false);
function value_in($element_name, $xml, $content_only = true)
{
if ($xml == false)
{
return false;
}
$found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'.'</'.$element_name.'>#s', $xml, $matches);
if ($found != false)
{
if ($content_only)
{
return $matches[1]; //ignore the enclosing tags
}
else
{
return $matches[0]; //return the full pattern match
}
}
// No match found: return false.
return false;
}
function loadpage()
{
echo "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 95%\" >";
echo "<tr class=\"info-row\" bgcolor=#252525 style=\"color:white; height: 15px;\">";
echo "<td style=\"width: 14%; height: 10px; padding-left: 5px;\"><b>Preview</b></td>";
echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Live</b></td>";
echo "<td style=\"width: 36%; height: 10px; padding-left: 5px;\"><b>Stream</b></td>";
echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Viewers</b></td>";
echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Time online</b></td>";
echo "</tr>";
addrow(107473,10,"Osuryn","Osuryn is streaming random games live",false);
addrow(210320,28,"Dennojj","Dennojj is streaming PS3 games",true);
echo "</table>";
}
function addrow($streamID, $streamPage , $streamName , $streamSlogan, $odd)
{
if ($odd)
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7>";
}
else
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF>";
}
echo "<td style=\"width: 14%;\"><img src=\"http://img.hw.own3d.tv/live/live_tn_".$streamID."_.jpg\" style=\"height: 72px;\" \></td>";
echo "<td style=\"width: 10%; padding-left: 5px;\"><br><br><b>".getLiveStatus($streamID)."</b></td>";
echo "<td style=\"width: 36%; vertical-align: top; padding-top: 6px; padding-right: 6px;\">";
echo "<div><br><a href=\"http://brokendiamond.org/?q=node/$streamPage\">$streamName</a></div>";
echo "<div style=\"padding-top: 6px; font-size: 11px;\">$streamSlogan</div>";
echo "</td>";
echo "<td style=\"width: 10%; padding-left: 5px;\"><br><br>".getLiveViews($streamID)."</td>";
echo "<td style=\"width: 10%; padding-left: 5px;\"><br><br>".getOnTime($streamID)." minutes online</td>";
echo "</tr>";
}
function getLiveStatus($streamID)
{
$request = 'http://api.own3d.tv/liveCheck.php?live_id='.$streamID;
$arg = '240';
$session = curl_init($request.$arg);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
if (preg_match("/true/",$response, $result))
{
$streamStatus="Live";
}
else
{
$streamStatus="Offline";
}
return $streamStatus;
}
function getLiveViews($StreamID)
{
$request = 'http://api.own3d.tv/liveCheck.php?live_id='.$StreamID;
$arg = '240';
$session = curl_init($request.$arg);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
$viewStatus =value_in('liveViewers', $response) + "";
return $viewStatus;
}
function getOnTime($StreamID)
{
$request = 'http://api.own3d.tv/liveCheck.php?live_id='.$StreamID;
$arg = '240';
$session = curl_init($request.$arg);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
$onStatus =value_in('LiveDuration', $response) + "";
return $onStatus;
}
?>
The reason it's not refreshing is because <?php loadpage() ?>
will only run when the page starts, not any subsequent refresh (every minute, it looks like). You'd want to make a request to another page to get that content every time after you initially have it.
jQuery:
$.get('yoururl.php', function(data) { /* Do stuff with `data` here */ });
As far as speed...What else is going on in that whole page? Between the 3 requests, you're at ~700ms, which accounts for close to a second of the 2/3, but not quite. I'm assuming there's some other drupal stuff going on?
Caching, yes, do that! There are several options you have: http://en.wikipedia.org/wiki/List_of_PHP_accelerators / Memcached are the most robust to use. There is another option of saving the response to a file and reading from the file if it's less than a minute old, otherwise make the request and overwrite it. This would be the simplest and would remove the ~700ms in web requests.