duaner5714 2012-02-11 10:37
浏览 33
已采纳

PHP如何更快地执行这些检查/或加快页面加载?

I'm new to PHP and I am wondering what is either a better way to perform this operation? The PHP uses 2 API's provided by own3d.tv and justin.tv to check a stream's status whether it's online/offline. The way I implemented it is horrible, since if you load the page, it checks each and every stream within the PHP individually and then only displays the page and the results. My question is, how can I load the page either a) quicker or b) using jquery or something to display the page first, but by each stream it would say "Loading..." and when it's actually checked it, display the result like it is below.

Here's all the PHP in mention.

<html>
<body>
<title>Streams</title>

<?php

function  pageTime()
{
static $_pt;
    if($_pt == 0) $_pt = microtime(true);
    else return (string)(round(microtime(true)-$_pt ,3));
}

pageTime();

//justin.tv
echo '<b>Justin.tv: </b> <br>';

//eg.demon
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=dotademon", 0, null, null);
$json_array = json_decode($json_file, true);

if ($json_array[0]['name'] == 'live_user_dotademon') {
echo 'EG.Demon <sup><b>Popular</b></sup> - <font color="green"><b>Online</b></font> - Viewers: '; echo ($json_array[0]['channel_count']); echo ' - <a href="http://www.justin.tv/dotademon/">Watch now!</a> <br>';
}
else {
echo 'EG.Demon <sup><b>Popular</b></sup> - <font color="red"><b>Offline</b></font> <br>';
}

//mouz.trixi
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=trixilulz", 0, null, null);
$json_array = json_decode($json_file, true);

if ($json_array[0]['name'] == 'live_user_trixilulz') {
echo 'Mouz.Trixi - <font color="green"><b>Online</b></font> - Viewers: '; echo ($json_array[0]['channel_count']); echo ' - <a href="http://www.justin.tv/trixilulz/">Watch now!</a> <br>';
}
else {
echo 'Mouz.Trixi - <font color="red"><b>Offline</b></font> <br>';
}

//teamdignitascomewithme
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=leceweme", 0, null, null);
$json_array = json_decode($json_file, true);

if ($json_array[0]['name'] == 'live_user_leceweme') {
echo 'Dignitas.ComeWithMe - <font color="green"><b>Online</b></font> - Viewers: '; echo ($json_array[0]['channel_count']); echo ' - <a href="http://www.justin.tv/leceweme/">Watch now!</a> <br>';
}
else {
echo 'Dignitas.ComeWithMe - <font color="red"><b>Offline</b></font> <br>';
}

//thepremierleague
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=thepremierleague", 0, null, null);
$json_array = json_decode($json_file, true);

if ($json_array[0]['name'] == 'live_user_thepremierleague') {
echo 'The Premier League - <font color="green"><b>Online</b></font> - Viewers: '; echo ($json_array[0]['channel_count']); echo ' - <a href="http://www.justin.tv/thepremierleague/">Watch now!</a> <br>';
}
else {
echo 'The Premier League - <font color="red"><b>Offline</b></font> <br>';
}

//own3d.tv
echo ' <br>';
echo '<b>Own3d.tv: </b> <br>';

//joindota with tobi wan
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=10588");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'joinDOTA with Tobi Wan <sup><b>Popular</b></sup> - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/10588/">Watch now!</a> <br>';
}
else {
echo 'joinDOTA with Tobi Wan <sup><b>Popular</b></sup> - <font color="red"><b>Offline</b></font> <br>';
}

//navidendi
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=106735");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'Na`Vi.Dendi <sup><b>Popular</b></sup> - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/106735/">Watch now!</a> <br>';
}
else {
echo 'Na`Vi.Dendi <sup><b>Popular</b></sup> - <font color="red"><b>Offline</b></font> <br>';
}

//ddlink
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=117967");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'DD.Link <sup><b>Popular</b></sup> - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/117967">Watch now!</a> <br>';
}
else {
echo 'SK.Link <sup><b>Popular</b></sup> - <font color="red"><b>Offline</b></font> <br>';
}   

//gosugamers
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=155074");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'GosuGamers - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/155074/">Watch now!</a> <br>';
}
else {
echo 'GosuGamers - <font color="red"><b>Offline</b></font> <br>';
}

//mtwsynderen
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=179879");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'mTw.Synderen - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/179879">Watch now!</a> <br>';
}
else {
echo 'mTw.Synderen - <font color="red"><b>Offline</b></font> <br>';
}

//gosugamers with purge
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=155073");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'GosuGamers with Purge - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/155073">Watch now!</a> <br>';
}
else {
echo 'GosuGamers with Purge - <font color="red"><b>Offline</b></font> <br>';
}       

//dotaoommentarieswithluminous
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=160379");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'DotA Commentaries with Luminous - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/160379">Watch now!</a> <br>';
}
else {
echo 'DotA Commentaries with Luminous - <font color="red"><b>Offline</b></font> <br>';
}   

//teamdignitascomewithme
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=40553");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'Dignitas.ComeWithMe - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/40553">Watch now!</a> <br>';
}
else {
echo 'Dignitas.ComeWithMe - <font color="red"><b>Offline</b></font> <br>';
}   

//loda
$xml = simplexml_load_file("http://api.own3d.tv/liveCheck.php?live_id=73096");
$resultlive = $xml->xpath("/own3dReply/liveEvent/isLive");
$resultviewers = $xml->xpath("/own3dReply/liveEvent/liveViewers");

if ($resultlive[0] == 'true'){
echo 'Loda - <font color="green"><b>Online</b></font> - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/73096">Watch now!</a> <br>';
}
else {
echo 'Loda - <font color="red"><b>Offline</b></font> <br>';
}   
echo ' <br/>';
echo "The statuses were primitively checked in ". pageTime()." seconds.";   
?>
</body>
</html>
  • 写回答

3条回答 默认 最新

  • dongzhan1570 2012-02-11 10:43
    关注

    This is an absolute most basic example, using AJAX and jQuery

    jQuery on the main page:

    $(document).ready( function() {
       $("#stream1").load("stream1.php"); 
       $("#stream2").load("stream2.php"); 
       ...
    });  
    

    HTML on main page:

    <div id="stream1">Loading...</div>
    <div id="stream2">Loading...</div>
    ...
    

    Where stream1.php, stream2.php are files using the code you posted for each individual stream.

    Consider using the $.ajax method in jQuery and playing around with ways to make it more functional. Should get you started.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要