PHP下查询游戏《Minecraft》多人游戏 服务器的人数。

废话不多说,直接上图:

PHP下查询游戏《Minecraft》多人游戏 服务器的人数。

PHP下查询游戏《Minecraft》多人游戏 服务器的人数。

 作为一个优雅的Minecraft服务器,肯定需要官网的嗯。

很多服务器的官网都有显示当前服务器在线人数,延迟,每一个子服在线人数,甚至出了个流量图。

我们不搞花里胡哨的查询人数,直接用最简单的代码去完成一个简约的人数查询。

很好,让我们来搞吧!

首先我们需要引用一个外部PHP文件。

名字可以起做STATUS.CLASS.php

然后里面加入如下代码:

<?php
 
    /**
    * Minecraft服务器状态查询
    * @作者 Julian Spravil <> Git地址:https://github.com/FunnyItsElmo
    * @本库免费使用,但不要删除作者和版权。  8     */
    class MinecraftServerStatus {
 
        private $timeout;
 
        public function __construct($timeout = 2) {
            $this->timeout = $timeout;
        }
 
        public function getStatus($host = ‘127.0.0.1‘, $version = ‘1.7.*‘ , $port = 25565) {
 
            if (substr_count($host , ‘.‘) != 4) $host = gethostbyname($host);
 
            $serverdata = array();
            $serverdata[‘hostname‘] = $host;
            $serverdata[‘version‘] = false;
            $serverdata[‘protocol‘] = false;
            $serverdata[‘players‘] = false;
            $serverdata[‘maxplayers‘] = false;
            $serverdata[‘motd‘] = false;
            $serverdata[‘motd_raw‘] = false;
            $serverdata[‘favicon‘] = false;
            $serverdata[‘ping‘] = false;
 
            $socket = $this->connect($host, $port);
 
            if(!$socket) {
                return false;
            }
 
            if(preg_match(‘/1.7|1.8/‘,$version)) {
 
                $start = microtime(true);
 
                $handshake = pack(‘cccca*‘, hexdec(strlen($host)), 0, 0x04, strlen($host), $host).pack(‘nc‘, $port, 0x01);
 
                socket_send($socket, $handshake, strlen($handshake), 0); //give the server a high five
                socket_send($socket, "\x01\x00", 2, 0);
                socket_read( $socket, 1 );
 
                $ping = round((microtime(true)-$start)*1000); //calculate the high five duration
 
                $packetlength = $this->read_packet_length($socket);
 
                if($packetlength < 10) {
                    return false;
                }
 
                socket_read($socket, 1);
 
                $packetlength = $this->read_packet_length($socket);
 
                $data = socket_read($socket, $packetlength, PHP_NORMAL_READ);
 
                if(!$data) {
                    return false;
                }
 
                $data = json_decode($data);
 
                $serverdata[‘version‘] = $data->version->name;
                $serverdata[‘protocol‘] = $data->version->protocol;
                $serverdata[‘players‘] = $data->players->online;
                $serverdata[‘maxplayers‘] = $data->players->max;
 
                $motd = $data->description;
                $motd = preg_replace("/(§.)/", "",$motd);
                $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);
 
                $serverdata[‘motd‘] = $motd;
                $serverdata[‘motd_raw‘] = $data->description;
                $serverdata[‘favicon‘] = $data->favicon;
                $serverdata[‘ping‘] = $ping;
 
            } else {
 
                $start = microtime(true);
 
                socket_send($socket, "\xFE\x01", 2, 0);
                $length = socket_recv($socket, $data, 512, 0);
 
                $ping = round((microtime(true)-$start)*1000);//calculate the high five duration
         
                if($length < 4 || $data[0] != "\xFF") {
                    return false;
                }
 
                $motd = "";
                $motdraw = "";
 
                //Evaluate the received data
                if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){
 
                    $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), ‘UTF-8‘, ‘UCS-2‘));
                    $motd = $result[1];
                    $motdraw = $motd;
 
                } else {
 
                    $result = explode(‘§‘, mb_convert_encoding(substr((String)$data, 3), ‘UTF-8‘, ‘UCS-2‘));
                        foreach ($result as $key => $string) {
                            if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
                                $motd .= ‘§‘.$string;
                            }
                        }
                        $motdraw = $motd;
                    }
 
                    $motd = preg_replace("/(§.)/", "", $motd);
                    $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd); //Remove all special characters from a string
 
                    $serverdata[‘version‘] = $result[0];
                    $serverdata[‘players‘] = $result[sizeof($result)-2];
                    $serverdata[‘maxplayers‘] = $result[sizeof($result)-1];
                    $serverdata[‘motd‘] = $motd;
                    $serverdata[‘motd_raw‘] = $motdraw;
                    $serverdata[‘ping‘] = $ping;
 
            }
 
            $this->disconnect($socket);
 
            return $serverdata;
 
        }
 
        private function connect($host, $port) {
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_connect($socket, $host, $port);
            return $socket;
        }
 
        private function disconnect($socket) {
            if($socket != null) {
                socket_close($socket);
            }
        }
 
        private function read_packet_length($socket) {
            $a = 0;
            $b = 0;
            while(true) {
                $c = socket_read($socket, 1);
                if(!$c) {
                    return 0;
                }
                $c = Ord($c);
                $a |= ($c & 0x7F) << $b++ * 7;
                if( $b > 5 ) {
                    return false;
                }
                if(($c & 0x80) != 128) {
                    break;
                }
            }
            return $a;
        }
 
    }
?>

很好,我们继续。

再新建一个php文件,名字和上面一样随意。然后复制如下代码到里面。

<?php
    include_once ‘status.class.php‘;
    
    $status = new MinecraftServerStatus(); // 类
    $response = $status-> getStatus(‘s1.devicloud.cn‘, 25565); // 服务器地址

if(!$response) {
    echo"服务器可能离线!";
} else {
    echo"".$response[‘players‘]."";
}
 
?>

好,就这样。

其中include_once ‘status.class.php‘;

是上面引用PHP库的相对路径。

如果那个PHP库在同级目录的libraries文件夹下,那就应该这样写:

include_once ‘/libraries/status.class.php‘;

然后就可以在你的网页内调用这段代码啦!

调用:

PHP下查询游戏《Minecraft》多人游戏 服务器的人数。

结果:

PHP下查询游戏《Minecraft》多人游戏 服务器的人数。

相关推荐