简体   繁体   English

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

[英]PHP How do I perform these checks faster/or load the page faster?

I'm new to PHP and I am wondering what is either a better way to perform this operation? 我是PHP的新手,我想知道什么是更好的方法来执行此操作? The PHP uses 2 API's provided by own3d.tv and justin.tv to check a stream's status whether it's online/offline. PHP使用own3d.tv和justin.tv提供的2个API来检查流的状态,无论它是在线还是离线。 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. 我实现它的方式很糟糕,因为如果你加载页面,它会单独检查PHP中的每一个流,然后只显示页面和结果。 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. 我的问题是,如何加载页面a)更快或b)使用jquery或其他东西首先显示页面,但每个流它会说“正在加载...”,当它实际检查它时,显示结果喜欢它在下面。

Here's all the PHP in mention. 这是提到的所有 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>

This is an absolute most basic example, using AJAX and jQuery 这是一个绝对最基本的例子,使用AJAX和jQuery

jQuery on the main page: 主页上的jQuery:

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

HTML on main page: 主页上的HTML:

<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. 其中stream1.phpstream2.php是使用您为每个流发布的代码的文件。

Consider using the $.ajax method in jQuery and playing around with ways to make it more functional. 考虑在jQuery中使用$ .ajax方法,并尝试使其更具功能性。 Should get you started. 应该让你开始。

To make this even faster I would suggest keeping your script how it is and running a cronjob every 5 mins or so, and save the online offline users in a local database. 为了使速度更快,我建议保持脚本的运行方式并每5分钟左右运行一次cronjob,并将在线离线用户保存在本地数据库中。 This will be the fastest possible way because query a local database is way faster than relying on a third party site to respond. 这将是最快的方式,因为查询本地数据库比依赖第三方站点响应更快。

You can do so by using jQuery's ajax method . 您可以使用jQuery的ajax方法完成此操作 This method has a callback called success, which is where you would replace the "Loading..." to "Online" or "Offline" depending on the result. 此方法有一个名为success的回调,您可以根据结果将“Loading ...”替换为“Online”或“Offline”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM