Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP ping Minecraft服务器:小错误_Php_Html_Minecraft - Fatal编程技术网

用PHP ping Minecraft服务器:小错误

用PHP ping Minecraft服务器:小错误,php,html,minecraft,Php,Html,Minecraft,作为测试,我正在尝试将数据从Minecraft服务器输入我的网页。代码如下: <?php $host = "hub.cobalt-mc.com"; $port = 25565; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $connected = socket_connect($socket, $host, $port); if ($connected) { $ping_start = microtime(true); s

作为测试,我正在尝试将数据从Minecraft服务器输入我的网页。代码如下:

<?php $host = "hub.cobalt-mc.com";
$port = 25565;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connected = socket_connect($socket, $host, $port);
if ($connected) {
$ping_start = microtime(true);
socket_send($socket, "\xFE", 1, 0);
$data = "";
$result = socket_recv($socket, &$data, 1024, 0);$ping_end = microtime(true);
socket_close($socket);

if ($result != false && substr($data, 0, 1) == "\xFF") {
$info = explode("\xA7", mb_convert_encoding(substr($data,1), "iso-8859-1","utf-16be"));
$serverName = substr($info[0], 1);
$playersOnline = $info[1];
$playersMax = $info[2];
$ping = round(($ping_end - $ping_start) * 1000);
echo     "<table class=\"table table-hover\">
<th>Server Address</th>
<th>Status</th>
<th>Rank</th>
<th>Total DP</th>

<tr>
<td>$host:$ip</td>
if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
<td></td>
<td></td>
</tr>

<tr>
<td>$host:$ip</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>";
                            }

 else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to receive data";
 }
 } else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to send data";
 }?>

代码有问题吗?

您的回音在$ping=round之后立即开始。。。行未在if语句之前终止。。。你不能只在回声的中间掉一个I/P 该行内容如下:

<td>$host:$ip</td>
应该是:

<td>$host:$ip</td>";
然后,您需要在if语句之后执行另一个echo

代码中似乎有无数其他问题会阻止你做你想做的事


正如注释中所述,正确的代码格式将大大有助于解决这些错误。。。使用一个为代码着色的编辑器,这样的错误会向你跳出来

您的错误消息涉及第27行:

Parse error: syntax error, unexpected '>' in /home/mctronco/public_html/leaderboards/serverping.php on line 27
所以目前还不清楚具体哪一行有问题。因此,我建议您更改以下所有内容,因为您的编码非常复杂。这不是我的理想,但应该有效

if ($result != false && substr($data, 0, 1) == "\xFF") {
$info = explode("\xA7", mb_convert_encoding(substr($data,1), "iso-8859-1","utf-16be"));
$serverName = substr($info[0], 1);
$playersOnline = $info[1];
$playersMax = $info[2];
$ping = round(($ping_end - $ping_start) * 1000);

echo "<table class=\"table table-hover\">";
echo "<th>Server Address</th>";
echo "<th>Status</th>";
echo "<th>Rank</th>";
echo "<th>Total DP</th>";

echo "<tr>";
echo "<td>$host:$ip</td>";
if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
echo "<td></td>";
echo "<td></td>";
echo "</tr>";

echo "<tr>";
echo "<td>".$host.":".$ip."</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
                            }

 else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to receive data";
 }
 } else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to send data";
 }

?>

您的代码中存在多个问题,但我将指出一个重要问题。不能将纯PHP放在字符串或回音的中间。 旧代码

<tr>
<td>$host:$ip</td>
if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
<td></td>
新代码

<tr>
<td>{$host}:{$ip}</td>";

if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}

echo "
<td></td>

最好是在echo之外执行if语句,将其结果存储在变量中,然后将变量放入echo中。当你像echo一样在双引号中时,你可以让PHP显示变量的内容,但它不会执行语句。

我知道你是新手,但如果你想让别人帮你调试,如果你正确排列代码,你会得到更多帮助。。。当你在它的时候,你可能会发现错误。你试图在一个简洁的感谢中增加更多的PHP代码。我还在学PHP,所以我不是最好的。有时查看代码确实有帮助。我重新编码了这段代码&所有的echo内容都是一团糟。那就在球上!但您的示例应该是:.$host...$ip@JakeGould:我更喜欢你使用的语法,但是由于整个echo字符串都用双引号括起来,变量替换就可以了。。。
<tr>
<td>{$host}:{$ip}</td>";

if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}

echo "
<td></td>