0

所以基本上,我对 PHP 一无所知,但不知何故,我设法让这个脚本工作。
(脚本会检查直播是否在线 [在 www.own3d.tv 上],如果在线,则显示主播和观众的姓名。如果离线,则不显示任何内容。)

我将其插入到我的 Wordpress 侧边栏中。但问题是,当我打开我的页面时,所有其他内容都会加载,但侧边栏需要几秒钟才能加载。我想检查直播需要很长时间。
那么,有没有办法让它加载得更快,或者让它先加载侧边栏上的其他东西?

提前感谢您的回复。
问候

<html>
<body>
<p style="width:300px; height:250px; padding:1px; border:5px solid #66a">
<?php





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

pageTime();





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

if ($resultlive[0] == 'true'){
echo 'Froggen - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/112348/">Watch now!</a> <br>';
}
else {
echo '';
}



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

if ($resultlive[0] == 'true'){
echo 'Athene - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/42146/">Watch now!</a> <br>';
}
else {
echo '';
}




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

if ($resultlive[0] == 'true'){
echo 'Guardsman Bob - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/34046/">Watch now!</a> <br>';
}
else {
echo '';
}



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

if ($resultlive[0] == 'true'){
echo 'The Rain Man - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/38853/">Watch now!</a> <br>';
}
else {
echo '';
}


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

if ($resultlive[0] == 'true'){
echo 'TheOddOne - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/6416/">Watch now!</a> <br>';
}
else {
echo '';
}


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

if ($resultlive[0] == 'true'){
echo 'Dyrus - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/37905/">Watch now!</a> <br>';
}
else {
echo '';
}


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

if ($resultlive[0] == 'true'){
echo 'Xpecial - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/10953/">Watch now!</a> <br>';
}
else {
echo '';
}


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

if ($resultlive[0] == 'true'){
echo 'Chaox - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/33356/">Watch now!</a> <br>';
}
else {
echo '';
}


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

if ($resultlive[0] == 'true'){
echo 'Wickd - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/18755/">Watch now!</a> <br>';
}
else {
echo '';
}


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

if ($resultlive[0] == 'true'){
echo 'Lapaka - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/214/">Watch now!</a> <br>';
}
else {
echo '';
}



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

if ($resultlive[0] == 'true'){
echo 'Snoopeh - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/112350/">Watch now!</a> <br>';
}
else {
echo '';
}

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

if ($resultlive[0] == 'true'){
echo 'Dedrayon - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/265558/">Watch now!</a> <br>';
}
else {
echo '';
}



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

if ($resultlive[0] == 'true'){
echo 'Malaco - Viewers: '; echo ($resultviewers[0]); echo ' - <a href="http://www.own3d.tv/live/259259/">Watch now!</a> <br>';
}
else {
echo '';
}




?>
</p>
</body>
</html>
4

2 回答 2

0

您应该缓存这些 simplexml_load_file() 调用的结果。第一次调用会花费正常的时间,但随后的调用会加载缓存的数据并执行得非常快。

于 2012-04-06T01:33:37.033 回答
0

尝试在侧边栏中创建一个简单的文本小部件并将其粘贴到内容中。只需确保您的主题已安装 jQuery:

Froggen - Viewers: <a class="live-viewers" href="http://www.own3d.tv/live/112348/">Loading...</a> 

<script type='text/javascript'>
$(document).ready(function () {
    $.get("http://api.own3d.tv/liveCheck.php?live_id=112348", function (res) {
    var xml = $.parseXML(res);
    $('.live-viewers').text(res.own3dReply.liveEvent.liveViewers[0];
});
</script>
于 2012-04-05T23:36:18.913 回答