Skip to content

Instantly share code, notes, and snippets.

@zerotri
Forked from dalbothek/phpmcinfo.php
Created September 14, 2011 10:14
Show Gist options
  • Save zerotri/1216252 to your computer and use it in GitHub Desktop.
Save zerotri/1216252 to your computer and use it in GitHub Desktop.
PHP Minecraft Info Query
<?php
class Socket
{
private $sock;
public function __construct()
{
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$this->sock)
throw new Exception("Socket.create() failed; reason: " . socket_strerror(socket_last_error($this->sock)) . "\n");
}
public function connect($host = "127.0.0.1", $port = 0)
{
if(socket_connect($this->sock, $host, $port) === false)
throw new Exception("Socket.connect() failed; reason: " . socket_strerror(socket_last_error($this->sock)) . "\n");
}
public function recv($length)
{
$buffer = "";
if (false !== ($bytes = socket_recv($this->sock, $buffer, $length, MSG_WAITALL))) {
return $buffer;
} else {
throw new Exception("Socket.recv() failed; reason: " . socket_strerror(socket_last_error($this->sock)) . "\n");
}
}
public function send($string)
{
$length = strlen($string);
socket_write( $this->sock, $string, $length);
}
public function close()
{
socket_close($this->sock);
}
};
function getInfo()
{
$buf = "";
$host = "66.172.10.67";
$port = 25565;
$socket = new Socket();
$socket->connect($host, $port);
$socket->send(chr(0xfe));
$response = $socket->recv(1);
if(ord($response) != 0xFF)
throw new Exception("Assert failed. Response: $response");
$len = unpack("n",$socket->recv(2));
$response = $socket->recv($len[1]*2);
$prestore = mb_split("\xc2\xA7", utf8_encode($response));
return array( 'name' => $prestore[0],
'online_players' => $prestore[1],
'maximum_players' => $prestore[2]);
}
echo Print_r(getInfo(),true);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment