类
/**
* 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-&gt; getStatus('pvp24.com',25565); 而不是 $ response = $ status-&gt; getStatus('$ IP',$端口);
它似乎有效
此外:
$GetInfo = $db->query("SELECT * FROM config");
$GI = $GetInfo->fetch_array(MYSQLI_BOTH);
答案 0 :(得分:1)
在PHP中,single-quoted strings不受变量扩展的影响。字符串文字'$IP'
表示包含三个字符的字符串:$
,I
和P
。 ('$IP' === "\$IP"
)
根本不需要引号字符。只需$IP
即可。如果你想使用引号字符,那么你必须使用双引号("$IP"
),但是当字符串中包含的唯一内容是变量时,没有理由这样做 - 只需使用变量。 (这不完全正确:"$foo"
是将$foo
转换为字符串的合法方式,但这不是必需的。)