2012-02-11 6 views
0

私はPHPを初めて使いましたが、この操作を実行するためのより良い方法は何ですか? PHPは、own3d.tvとjustin.tvによって提供される2つのAPIを使用して、オンライン/オフラインのいずれかのストリームのステータスをチェックします。私が実装した方法は恐ろしいことです。なぜなら、ページをロードすると、PHP内のすべてのストリームを個別にチェックし、ページと結果のみを表示するからです。私の質問は、ページを最初に表示するにはどうすればいいですか?または最初にページを表示するために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> 
+1

これはおそらく[Codereview](http://codereview.stackexchange.com) – Grexis

+0

に適しています。ヘルプが必要な場合は、さらに詳しい説明が必要です –

答えて

2

これは、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メソッドを使用して、より機能的にする方法を検討してください。あなたを始めなければならない。

+0

回答メイトに感謝します。これは完璧に機能し、 ! – user1104783

0

これは、jQueryのajax methodを使用して行うことができます。このメソッドには成功と呼ばれるコールバックがあります。これは結果に応じて「読み込み中...」を「オンライン」または「オフライン」に置き換えます。

0

これをもっと速くするには、スクリプトの状態を維持して5分ごとにcronjobを実行し、オンラインのオフラインユーザーをローカルデータベースに保存することをお勧めします。これは、可能なかぎり最速の方法です。ローカルデータベースへのクエリは、サードパーティのサイトに応答するよりも高速です。

関連する問題