-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

In PHP, single-quoted strings are not subject to variable expansion. The string literal '$IP' represents a string containing three characters: $, I, and P, in that order. ('$IP' === "\$IP")

You don't need the quote characters at all. Just $IP will suffice. If you want to use quote characters then you must use double quotes ("$IP"), but there is no reason to do so when the only thing contained in the string is a variable -- just use the variable. (This is not completely true: "$foo" is a legitimate way to convert $foo to a string, but that is not necessary here.)

于 2013-07-24T21:09:43.620 に答える