-6

我正在尝试将来自 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";
 }?>

我得到错误:

Parse error: syntax error, unexpected '>' in /home/mctronco/public_html/leaderboards/serverping.php on line 27

代码有问题吗?

4

3 回答 3

0

您在该$ping = round(...行之后开始的回声不会在 if 语句之前终止......您不能只是if在中间放置一个echo

行内容如下:

<td>$host:$ip</td>

应该:

<td>$host:$ip</td>";

然后你需要在 if 语句之后再做一次 echo ...

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

如评论中所述,正确的代码格式将大大有助于解决这些错误......使用为代码着色的编辑器,这样的错误会跳出来......

于 2013-02-01T19:37:02.990 回答
0

您的错误消息指的是第 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";
 }

?>
于 2013-02-01T19:38:27.617 回答
0

您的代码中有多个问题,但我要指出一个重要问题。您不能将纯 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 中,那会更好。当您在双引号内时(如您的回声),您可以让 PHP 显示变量的内容,但它不会执行语句。

于 2013-02-01T19:44:52.567 回答