Skip to content

Instantly share code, notes, and snippets.

@malja
Created September 5, 2018 20:04
Show Gist options
  • Save malja/d9ffad09869537abaa8684555fc69642 to your computer and use it in GitHub Desktop.
Save malja/d9ffad09869537abaa8684555fc69642 to your computer and use it in GitHub Desktop.
PHP class for getting minecraft server status - number of players, MOTD, version and ping
<?php
class ServerStatus {
/*private $motd;
private $address;
private $ip;
private $port;
private $version;
private $players;
private $max_players;
private $ping;*/
private $status = array();
public function __construct( $status ) {
$this->status = $status;
}
public function getMOTD() {
return $this->status["motd"];
}
public function getAddress() {
return $this->status["address"];
}
public function getPort() {
return $this->status["port"];
}
public function getVersion() {
return $this->status["version"];
}
public function getCurrentPlayers() {
return $this->status["players"];
}
public function getMaxPlayers() {
return $this->status["max_players"];
}
public function getPing( $round = 0 ) {
if ( is_numeric( $round ) AND $round >= 0 ) {
return round( $this->status["ping"], $round );
} else {
return $this->status["ping"];
}
}
}
class ServerStatusQuery {
public function __construct() {
}
public function getServerInfo( $address = "127.0.0.1", $port = 25565, $timeout = 5 ) {
// Pokud není rovnou uvedena IP
if( !filter_var( $address, FILTER_VALIDATE_IP ) ) {
// Převedeme adresu na IP
$address = gethostbyname( $address );
}
$ping_start = microtime(true);
$socket = stream_socket_client("tcp://" . $address . ":" . $port, $errorNumber, $errorString, $timeout );
if ( !$socket ) {
return false;
} else {
stream_set_timeout( $socket, $timeout );
fwrite ( $socket, "\XFE\01" );
$data = fread( $socket, 2048 );
fclose( $socket );
$ping = microtime(true) - $ping_start;
if( $data == null ) {
return false;
}
if ( substr( (String) $data, 3, 5) == "\00\xa7\x00\x31\x00" ) {
$content = explode( "\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2' ) );
$motd = preg_replace("/(§.)/", "",$content[1]);
} else {
$content = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
$motd = "";
foreach ($content as $key => $string) {
if($key != sizeof($content)-1 && $key != sizeof($content)-2 && $key != 0) {
$motd .= '§'.$string;
}
}
$motd = preg_replace("/(§.)/", "", $motd);
}
$motd = preg_replace("/[^[:alnum:][:punct:] ]/", "", $motd);
$info = array(
"motd" => $motd,
"version" => $content[ 0 ],
"players" => $content[ 2 ],
"max_players" => $content[ 3 ],
"address" => $address,
"ping" => $ping
);
return new ServerStatus( $info );
}
}
};
$status = new ServerStatusQuery();
$info = $status->getServerInfo("mc27.crew.sk", 25580);
echo "MOTD: " . $info->getMOTD() . "<br/>";
echo "Version: " . $info->getVersion() . "<br/>";
echo "Players: " . $info->getCurrentPlayers() . "/" . $info->getMaxPlayers() . "<br/>";
echo "IP/Address: " . $info->getAddress() . "<br/>";
echo "Ping: " . $info->getPing() . "<br/>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment