我正在尝试在PHP中设置一个方法来ping Mojang服务,然后将结果输出到我正在处理的项目的文本文件中。
我在互联网上借用了部分代码,并创建了这个代码:
<?php
//Puts the received JSON from status.mojang.com to a variable;
$json = file_get_contents("http://status.mojang.com/check");
//If the JSON is NOT empty, then decode it;
if(!empty($json)) {
$result = json_decode($json,true);
}
//Array to store all 8 JSON mojang status web services;
$server = array("minecraft.net", "login.minecraft.net", "session.minecraft.net", "account.mojang.com", "auth.mojang.com", "skins.minecraft.net", "authserver.mojang.com", "sessionserver.mojang.com");
//We gotta add into $result[0] a number for each server([0] for the first, [1] for the second, etc.);
$i = 0;
//For each server from the array, draw a DIV, which the background-color corresponds the status(Green:Online, Yellow:Semi-Online, Red:Offline);
foreach ($server as $address) {
if (isset($result[$i][$address]) && $result[$i][$address]=="green") {
//Echoes the ONLINE status;
echo "<div style='background-color: lightgreen;'>" . $address . "</div>";
}elseif($result[$i][$address]=="yellow"){
//Echoes the SEMI-ONLINE status;
echo "<div style='background-color: yellow;'>" . $address . "</div>";
}else{
//Echoes the OFFLINE status;
echo "<div style='background-color: red;'>" . $address . "</div>";
}
//Increment 1 to $i, so then, you can draw the status for the second server, and so on;
$i++;
}
?>
现在,在进行测试时,它会将“minecraft.net”显示为在线,但在检查我从(http://status.mojang.com/check)获取JSON的位置时,它会将所有服务显示为在线。
有谁知道我做错了什么?
由于
答案 0 :(得分:1)
运行代码时,它已经填满了通知:
注意:未定义的索引:login.minecraft.net
注意:未定义的索引:session.minecraft.net
注意:未定义的索引:account.mojang.com
等等。
问题是JSON是无序的,但您正在尝试按照您声明的顺序阅读它们:
'minecraft.net'
'login.minecraft.net'
'session.minecraft.net'
'account.mojang.com'
'auth.mojang.com'
'skins.minecraft.net'
'authserver.mojang.com'
'sessionserver.mojang.com'
但是当它们从结果转回数组时,它们按此顺序显示:
'minecraft.net'
'session.minecraft.net'
'account.mojang.com'
'auth.mojang.com'
'skins.minecraft.net'
'authserver.mojang.com'
'sessionserver.mojang.com'
'api.mojang.com'
'textures.minecraft.net'
您需要使用任何首先使用的服务器来使foreach
通用:
<?php
//Puts the received JSON from status.mojang.com to a variable;
$json = file_get_contents("http://status.mojang.com/check");
//If the JSON is NOT empty, then decode it;
if(!empty($json))
{
$result = json_decode($json, true);
}
//For each result given to us, draw a DIV, which the background-color corresponds the status(Green:Online, Yellow:Semi-Online, Red:Offline);
foreach($result as $address)
{
$server = array_keys($address)[0];
$colour = array_values($address)[0];
switch($colour)
{
case 'green': $colour = 'lightgreen'; break;
case 'red': break; //Do nothing, red is good
case 'yellow': break; //Do nothing, yellow is good
default: $colour = 'red'; break; //Something went wrong, assume it's down
}
echo "<div style=\"background-color: $colour;\">$server</div>" . PHP_EOL;
}
生成以下HTML:
<div style="background-color: lightgreen;">minecraft.net</div>
<div style="background-color: lightgreen;">session.minecraft.net</div>
<div style="background-color: lightgreen;">account.mojang.com</div>
<div style="background-color: lightgreen;">auth.mojang.com</div>
<div style="background-color: lightgreen;">skins.minecraft.net</div>
<div style="background-color: lightgreen;">authserver.mojang.com</div>
<div style="background-color: lightgreen;">sessionserver.mojang.com</div>
<div style="background-color: lightgreen;">api.mojang.com</div>
<div style="background-color: lightgreen;">textures.minecraft.net</div>