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

Posted

技术标签:

【中文标题】PHP 如何更快地执行这些检查/或更快地加载页面?【英文标题】:PHP How do I perform these checks faster/or load the page faster? 【发布时间】:2012-02-11 10:37:13 【问题描述】:

我是 php 新手,我想知道执行此操作的更好方法是什么? PHP 使用 own3d.tv 和 justin.tv 提供的 2 个 API 来检查流的状态是否在线/离线。我实现它的方式很糟糕,因为如果你加载页面,它会单独检查 PHP 中的每个流,然后只显示页面和结果。我的问题是,我怎样才能加载页面a)更快或b)使用jquery或其他东西首先显示页面,但是每个流都会说“正在加载......”并且当它实际检查时,显示结果就像下面一样。

这里是所有提到的 PHP。

<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>

【问题讨论】:

这可能更适合Codereview 如果你需要帮助,你需要更多的解释 【参考方案1】:

这是一个绝对最基本的例子,使用 AJAX 和 jQuery

主页上的jQuery:

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

主页上的 HTML:

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

其中stream1.phpstream2.php 是使用您为每个单独的流发布的代码的文件。

考虑在 jQuery 中使用 $.ajax 方法,并尝试使其更实用的方法。应该让你开始。

【讨论】:

感谢您的回复队友,这工作完美,绝对可以在此之上工作!【参考方案2】:

您可以使用 jQuery 的 ajax method 来实现。此方法有一个称为成功的回调,您可以根据结果将“正在加载...”替换为“在线”或“离线”。

【讨论】:

【参考方案3】:

为了让这个过程更快,我建议保持脚本的原样,每 5 分钟左右运行一次 cronjob,并将在线离线用户保存在本地数据库中。这将是最快的方法,因为查询本地数据库比依赖第三方站点响应要快得多。

【讨论】:

以上是关于PHP 如何更快地执行这些检查/或更快地加载页面?的主要内容,如果未能解决你的问题,请参考以下文章

更快地加载firebase图像数据

如何优化限制查询以便从庞大的表中更快地访问数据?

如何更快地加载 MKMapView?

如何在 python 中从 redshift 更快地处理数据?

如何更快地加载多个网格

DataTable:如何使页面加载更快?