-6

班级

    /**
     * Minecraft Server Status Query
     * @author Julian Spravil <julian.spr@t-online.de> https://github.com/FunnyItsElmo
     * @license Free to use but dont remove the author, license and copyright
     * @copyright © 2013 Julian Spravil
     */
    class statuss {
        private $timeout;

        /**
         * Prepares the class.
         * @param int    $timeout   default(3)
         */
        public function __construct($timeout = 3) {
            $this->timeout = $timeout;
        }

        /**
         * Gets the status of the target server.
         * @param string    $host    domain or ip address
         * @param int    $port    default(25565)
         */
        public function getStatus($host = '127.0.0.1', $port = 25565) {

            //Transform domain to ip address.
            if (substr_count($host , '.') != 4) $host = gethostbyname($host);

            //Get timestamp for the ping
            $start = microtime(true);

            //Connect to the server
            if(!$socket = @stream_socket_client('tcp://'.$host.':'.$port, $errno, $errstr, $this->timeout)) {

                //Server is offline
                return false;


            } else {

                stream_set_timeout($socket, $this->timeout);

                //Write and read data
                fwrite($socket, "\xFE\x01");
                $data = fread($socket, 2048);
                fclose($socket);
                if($data == null) return false;

                //Calculate the ping
                $ping = round((microtime(true)-$start)*1000);

                //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 = preg_replace("/(§.)/", "",$result[1]);

                }else{

                    $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));

                    $motd = "";
                    foreach ($result as $key => $string) {
                        if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
                            $motd .= '§'.$string;
                        }
                    }

                    $motd = preg_replace("/(§.)/", "", $motd);

                }
                //Remove all special characters from a string
                $motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);

                //Set variables
                $res = array();
                $res['hostname'] = $host;
                $res['version'] = $result[0];
                $res['motd'] = $motd;
                $res['players'] = $result[sizeof($result)-2];
                $res['maxplayers'] = $result[sizeof($result)-1];
                $res['ping'] = $ping;

                //return obj
                return $res;
            }

        }
    }

?>

回复:

$IP = $GI['ServerIP'];
         $Port = $GI['ServerPort'];
         $status = new statuss(); // call the class
         $response = $status->getStatus('$IP', $Port);   
if(!$response) {
echo"<h4 style='color:red;'> The Server is offline! </h4>";
} else {
echo"
<h4> Server Name:  ".$response['motd']."</h4>
<h4> Server IP: ".$response['hostname']." </h4>  <h4> Version: ".$response['version']." </h4> <h4> Status: <span style='color:green'>Online </span> </h4>
<h4> Online Players: ".$response['players']." </h4>
<h4> Maximum Players:  ".$response['maxplayers']." </h4>
<h4> Ping:  ".$response['ping']."</h4>";
}

由于某种原因,它显示服务器处于脱机状态。当我做$response = $status->getStatus('pvp24.com', 25565); 而不是$response = $status->getStatus('$IP', $Port);
它似乎有效

还:

    $GetInfo = $db->query("SELECT * FROM config");
$GI = $GetInfo->fetch_array(MYSQLI_BOTH);
4

1 回答 1

1

在 PHP 中,单引号字符串不受变量扩展的影响。字符串字面量'$IP'表示包含三个字符的字符串:$IP, 按此顺序。( '$IP' === "\$IP")

您根本不需要引号字符。就够$IP了。如果要使用引号字符,则必须使用双引号 ( "$IP"),但是当字符串中包含的唯一内容是变量时,没有理由这样做 - 只需使用变量即可。(这并不完全正确:这"$foo"是一种转换$foo为字符串的合法方式,但这里没有必要。)

于 2013-07-24T21:09:43.620 回答