我已经制作了一个用于直播布局的网页,有没有办法加快加载过程?因为加载有点慢到我的标准,加载需要大约5-10秒,这是慢的方法,我重写了很多代码,使方法更通用,但是这个过程仍然占用很长,也许问题在于第一种方法(value_in),但我确实不知道。
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!
<?php
header('Refresh: 60');
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;
}
loadpage();
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;
}
?>
答案 0 :(得分:1)
你是对的,5到10秒很慢。但它可能与您自己的代码无关。多次调用外部API是使速度减慢的部分。
如果事先知道您对外部Web服务进行的调用,则应该尝试每隔一分钟左右进行一次后台进程轮询并缓存结果。然后,您的实际脚本可以访问缓存的结果,这将快得多。
答案 1 :(得分:1)
使用缓存机制和时间戳超时。那就是你的数据保持更新,你的服务器一直没有大量的请求。
答案 2 :(得分:1)
数据的变化频率如何?如果它不经常更改,请有一个生成并保存文件ss html的计划任务(cron),以便浏览器只加载html。
答案 3 :(得分:0)
您正在访问外部API。你受其他网站的支配。