有效地加载大量PHP而不会影响页面加载时间

时间:2016-03-20 15:31:03

标签: php arrays json decode minecraft

以下是我为Minecraft服务器制作的状态页面的源代码。正如它目前所说的那样,它实际上同时ping所有16台服务器,并且正如您所期望的那样,页面离开页面的速度非常慢,并且会破坏整体的有用性。

我是否可以看到有关如何有效加载一个服务器状态的示例?我假设它会使用Ajax或其他东西。我的理想设置甚至不会检查服务器状态,直到页面完全加载。然后它将按顺序扫描每个服务器并显示状态。未完成的服务器会说加载,直到轮到他们查询。

提前感谢您的建议。厌倦了等待我的页面加载哈哈40秒。

<title>AvengeTech / Network Status</title>
<?php
include "layout/_header.php";

# Lobbies

$query_JSON_19132          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19132/mcpe"), true);
$query_OnlineStatus_19132  = $query_JSON_19132["status"];
$query_ServerVersion_19132 = $query_JSON_19132["version"];
$query_OnlinePlayers_19132 = $query_JSON_19132["players"]["online"];
$query_MaxPlayers_19132    = $query_JSON_19132["players"]["max"];

$query_JSON_19133          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19133/mcpe"), true);
$query_OnlineStatus_19133  = $query_JSON_19133["status"];
$query_ServerVersion_19133 = $query_JSON_19133["version"];
$query_OnlinePlayers_19133 = $query_JSON_19133["players"]["online"];
$query_MaxPlayers_19133    = $query_JSON_19133["players"]["max"];

# Factions

$query_JSON_19120          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19120/mcpe"), true);
$query_OnlineStatus_19120  = $query_JSON_19120["status"];
$query_ServerVersion_19120 = $query_JSON_19120["version"];
$query_OnlinePlayers_19120 = $query_JSON_19120["players"]["online"];
$query_MaxPlayers_19120    = $query_JSON_19120["players"]["max"];

$query_JSON_19121          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19121/mcpe"), true);
$query_OnlineStatus_19121  = $query_JSON_19121["status"];
$query_ServerVersion_19121 = $query_JSON_19121["version"];
$query_OnlinePlayers_19121 = $query_JSON_19121["players"]["online"];
$query_MaxPlayers_19121    = $query_JSON_19121["players"]["max"];

$query_JSON_19122          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19122/mcpe"), true);
$query_OnlineStatus_19122  = $query_JSON_19122["status"];
$query_ServerVersion_19122 = $query_JSON_19122["version"];
$query_OnlinePlayers_19122 = $query_JSON_19122["players"]["online"];
$query_MaxPlayers_19122    = $query_JSON_19122["players"]["max"];

$query_JSON_19134          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19134/mcpe"), true);
$query_OnlineStatus_19134  = $query_JSON_19134["status"];
$query_ServerVersion_19134 = $query_JSON_19134["version"];
$query_OnlinePlayers_19134 = $query_JSON_19134["players"]["online"];
$query_MaxPlayers_19134    = $query_JSON_19134["players"]["max"];

$query_JSON_19135          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19135/mcpe"), true);
$query_OnlineStatus_19135  = $query_JSON_19135["status"];
$query_ServerVersion_19135 = $query_JSON_19135["version"];
$query_OnlinePlayers_19135 = $query_JSON_19135["players"]["online"];
$query_MaxPlayers_19135    = $query_JSON_19135["players"]["max"];

$query_JSON_19136          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19136/mcpe"), true);
$query_OnlineStatus_19136  = $query_JSON_19136["status"];
$query_ServerVersion_19136 = $query_JSON_19136["version"];
$query_OnlinePlayers_19136 = $query_JSON_19136["players"]["online"];
$query_MaxPlayers_19136    = $query_JSON_19136["players"]["max"];

$query_JSON_19137          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19137/mcpe"), true);
$query_OnlineStatus_19137  = $query_JSON_19137["status"];
$query_ServerVersion_19137 = $query_JSON_19137["version"];
$query_OnlinePlayers_19137 = $query_JSON_19137["players"]["online"];
$query_MaxPlayers_19137    = $query_JSON_19137["players"]["max"];

$query_JSON_19138          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19138/mcpe"), true);
$query_OnlineStatus_19138  = $query_JSON_19138["status"];
$query_ServerVersion_19138 = $query_JSON_19138["version"];
$query_OnlinePlayers_19138 = $query_JSON_19138["players"]["online"];
$query_MaxPlayers_19138    = $query_JSON_19138["players"]["max"];

# Gunwars

$query_JSON_19140          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19140/mcpe"), true);
$query_OnlineStatus_19140  = $query_JSON_19140["status"];
$query_ServerVersion_19140 = $query_JSON_19140["version"];
$query_OnlinePlayers_19140 = $query_JSON_19140["players"]["online"];
$query_MaxPlayers_19140    = $query_JSON_19140["players"]["max"];

# PvP Biome

$query_JSON_19141          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19141/mcpe"), true);
$query_OnlineStatus_19141  = $query_JSON_19141["status"];
$query_ServerVersion_19141 = $query_JSON_19141["version"];
$query_OnlinePlayers_19141 = $query_JSON_19141["players"]["online"];
$query_MaxPlayers_19141    = $query_JSON_19141["players"]["max"];

# KitPvP

$query_JSON_19142          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19142/mcpe"), true);
$query_OnlineStatus_19142  = $query_JSON_19142["status"];
$query_ServerVersion_19142 = $query_JSON_19142["version"];
$query_OnlinePlayers_19142 = $query_JSON_19142["players"]["online"];
$query_MaxPlayers_19142    = $query_JSON_19142["players"]["max"];

# Prison

$query_JSON_19145          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19145/mcpe"), true);
$query_OnlineStatus_19145  = $query_JSON_19145["status"];
$query_ServerVersion_19145 = $query_JSON_19145["version"];
$query_OnlinePlayers_19145 = $query_JSON_19145["players"]["online"];
$query_MaxPlayers_19145    = $query_JSON_19145["players"]["max"];

$query_JSON_19146          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19146/mcpe"), true);
$query_OnlineStatus_19146  = $query_JSON_19146["status"];
$query_ServerVersion_19146 = $query_JSON_19146["version"];
$query_OnlinePlayers_19146 = $query_JSON_19146["players"]["online"];
$query_MaxPlayers_19146    = $query_JSON_19146["players"]["max"];

$query_JSON_19147          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19147/mcpe"), true);
$query_OnlineStatus_19147  = $query_JSON_19147["status"];
$query_ServerVersion_19147 = $query_JSON_19147["version"];
$query_OnlinePlayers_19147 = $query_JSON_19147["players"]["online"];
$query_MaxPlayers_19147    = $query_JSON_19147["players"]["max"];

$query_JSON_19148          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19148/mcpe"), true);
$query_OnlineStatus_19148  = $query_JSON_19148["status"];
$query_ServerVersion_19148 = $query_JSON_19148["version"];
$query_OnlinePlayers_19148 = $query_JSON_19148["players"]["online"];
$query_MaxPlayers_19148    = $query_JSON_19148["players"]["max"];
?>

<h1>Network Status</h1>
<hr>
<h3>Lobbies</h3>
<br>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style="font-weight:400;">Lobby</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19132</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19132 == TRUE) {
    echo "<p>$query_OnlinePlayers_19132/$query_MaxPlayers_19132</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19132 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Lobby 2</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19133</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19133 == TRUE) {
    echo "<p>$query_OnlinePlayers_19133/$query_MaxPlayers_19133</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19133 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<br>
<h3>Factions</h3>
<br>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Cyan</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19120</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19120 == TRUE) {
    echo "<p>$query_OnlinePlayers_19120/$query_MaxPlayers_19120</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19120 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Red</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19121</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19121 == TRUE) {
    echo "<p>$query_OnlinePlayers_19121/$query_MaxPlayers_19121</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19121 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Green</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19122</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19122 == TRUE) {
    echo "<p>$query_OnlinePlayers_19122/$query_MaxPlayers_19122</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19122 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Classic</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19134</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19134 == TRUE) {
    echo "<p>$query_OnlinePlayers_19134/$query_MaxPlayers_19134</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19134 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Classic 2</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19135</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19135 == TRUE) {
    echo "<p>$query_OnlinePlayers_19135/$query_MaxPlayers_19135</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19135 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Gold</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19136</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19136 == TRUE) {
    echo "<p>$query_OnlinePlayers_19136/$query_MaxPlayers_19136</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19136 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Purple</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19137</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19137 == TRUE) {
    echo "<p>$query_OnlinePlayers_19137/$query_MaxPlayers_19137</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19137 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Factions Blue</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19138</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19138 == TRUE) {
    echo "<p>$query_OnlinePlayers_19138/$query_MaxPlayers_19138</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19138 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<br>
<h3>GunWars</h3>
<br>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>GunWars</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19140</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19140 == TRUE) {
    echo "<p>$query_OnlinePlayers_19140/$query_MaxPlayers_19140</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19140 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<br>
<h3>PvP Biome</h3>
<br>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>PvP Biome</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19141</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19141 == TRUE) {
    echo "<p>$query_OnlinePlayers_19141/$query_MaxPlayers_19141</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19141 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<br>
<h3>KitPvP</h3>
<br>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>KitPvP</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19142</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19142 == TRUE) {
    echo "<p>$query_OnlinePlayers_19142/$query_MaxPlayers_19142</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19142 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<br>
<h3>Prison</h3>
<br>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Prison A</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19145</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19145 == TRUE) {
    echo "<p>$query_OnlinePlayers_19145/$query_MaxPlayers_19145</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19145 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Prison B</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19146</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19146 == TRUE) {
    echo "<p>$query_OnlinePlayers_19146/$query_MaxPlayers_19146</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19146 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Prison C</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19147</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19147 == TRUE) {
    echo "<p>$query_OnlinePlayers_19147/$query_MaxPlayers_19147</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19147 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<div class='paper'>
<div class='row'>
   <div class='col-sm-3'>
      <p style='font-weight:400;'>Prison D</p>
   </div>
   <div class='col-sm-3'>
      <p>play.avengetech.net:19148</p>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19148 == TRUE) {
    echo "<p>$query_OnlinePlayers_19148/$query_MaxPlayers_19148</p>";
} else {
    echo "<p>--/--</p>";
}
?>
   </div>
   <div class='col-sm-3'>
      <?php
if ($query_OnlineStatus_19148 == TRUE) {
    echo "<p style='color:#00aa00;'><i class='fa fa-circle'></i> Online</p>";
} else {
    echo "<p style='color:#aa0000;'><i class='fa fa-circle-o'></i> Unreachable</p>";
}
?>
   </div>
</div>
<br>
<?php
include "layout/_footer.php";
?> 

2 个答案:

答案 0 :(得分:1)

使用AJAX获取您要查找的信息。如果你想坚持使用PHP,你可以使用for循环并使其循环16次。

使用if ($query_OnlineStatus_19132 == TRUE) {更改if ($query_OnlineStatus_19132) { == TRUE是不必要的。

以下是如何使用For循环加速部分PHP:

<?php

include("layout/_header.php");

# Lobbies

//This will do it for server 19132 and 19133 as the $i<2 makes the loop stop at 2 loops and the $i++ makes the value 19132 up by 1 each loop
for($i=19132;$i<2;$i++){
    ${"query_JSON_".$i}          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:".$i."/mcpe"), true);
    ${"query_OnlineStatus_".$i}  = ${"query_JSON_".$i}["status"];
    ${"query_ServerVersion_".$i} = ${"query_JSON_".$i}["version"];
    ${"query_OnlinePlayers_".$i} = ${"query_JSON_".$i}["players"]["online"];
    ${"query_MaxPlayers_".$i}    = ${"query_JSON_".$i}["players"]["max"];
}

# Factions

for($i=19120;$i<8;$i++){
    ${"query_JSON_".$i}          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:".$i."/mcpe"), true);
    ${"query_OnlineStatus_".$i}  = ${"query_JSON_".$i}["status"];
    ${"query_ServerVersion_".$i} = ${"query_JSON_".$i}["version"];
    ${"query_OnlinePlayers_".$i} = ${"query_JSON_".$i}["players"]["online"];
    ${"query_MaxPlayers_".$i}    = ${"query_JSON_".$i}["players"]["max"];
}

# Gunwars

$query_JSON_19140          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19140/mcpe"), true);
$query_OnlineStatus_19140  = $query_JSON_19140["status"];
$query_ServerVersion_19140 = $query_JSON_19140["version"];
$query_OnlinePlayers_19140 = $query_JSON_19140["players"]["online"];
$query_MaxPlayers_19140    = $query_JSON_19140["players"]["max"];

# PvP Biome

$query_JSON_19141          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19141/mcpe"), true);
$query_OnlineStatus_19141  = $query_JSON_19141["status"];
$query_ServerVersion_19141 = $query_JSON_19141["version"];
$query_OnlinePlayers_19141 = $query_JSON_19141["players"]["online"];
$query_MaxPlayers_19141    = $query_JSON_19141["players"]["max"];

# KitPvP

$query_JSON_19142          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:19142/mcpe"), true);
$query_OnlineStatus_19142  = $query_JSON_19142["status"];
$query_ServerVersion_19142 = $query_JSON_19142["version"];
$query_OnlinePlayers_19142 = $query_JSON_19142["players"]["online"];
$query_MaxPlayers_19142    = $query_JSON_19142["players"]["max"];

# Prison

for($i=19145;$i<4;$i++){
    ${"query_JSON_".$i}          = json_decode(file_get_contents("https://mcapi.ca/query/play.avengetech.net:".$i."/mcpe"), true);
    ${"query_OnlineStatus_".$i}  = ${"query_JSON_".$i}["status"];
    ${"query_ServerVersion_".$i} = ${"query_JSON_".$i}["version"];
    ${"query_OnlinePlayers_".$i} = ${"query_JSON_".$i}["players"]["online"];
    ${"query_MaxPlayers_".$i}    = ${"query_JSON_".$i}["players"]["max"];
}

?>

答案 1 :(得分:1)

一个简单的例子 -

将网址存储在javascript对象中 -

var urls = {19132 : "https://mcapi.ca/query/play.avengetech.net:19132/mcpe",
            19133 : "https://mcapi.ca/query/play.avengetech.net:19133/mcpe",
            ...
           };

然后有一个函数来执行ajax请求 -

function mceAjax(id, URL_address){
    $.ajax({
         type: 'POST',
         url: URL_address,
         dataType: 'json',
         success: function (data) {
             if(data.status == TRUE) {
                 //put the id as the div id, so then you can use it in the selector
                 $('#'+id).html("<p>"+data.players.online+"/"+data.players.max+"</p>");
             }
             else {
                 $('#'+id).html("<p>--/--</p>");
             }
         }
     });
};

要检查每个服务器,您将遍历js对象 -

$(function() { //when the page is loaded/ready
    for(var id in urls){ //loop over each url
        mceAjax(id, urls[id]); //execute the Ajax function
    }
});