簡體   English   中英

PHP如何更快地執行這些檢查/或加快頁面加載?

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

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

這是一個絕對最基本的例子,使用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方法,並嘗試使其更具功能性。 應該讓你開始。

為了使速度更快,我建議保持腳本的運行方式並每5分鍾左右運行一次cronjob,並將在線離線用戶保存在本地數據庫中。 這將是最快的方式,因為查詢本地數據庫比依賴第三方站點響應更快。

您可以使用jQuery的ajax方法完成此操作 此方法有一個名為success的回調,您可以根據結果將“Loading ...”替換為“Online”或“Offline”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM