Skip to content

Instantly share code, notes, and snippets.

@dalbothek
Forked from zerotri/phpmcinfo.php
Created September 14, 2011 09:47
Show Gist options
  • Save dalbothek/1216213 to your computer and use it in GitHub Desktop.
Save dalbothek/1216213 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 = "127.0.0.1";
$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));
$len = $len[1];
$response = $socket->recv($len*2);
echo "Response: " . utf8_encode($response);
}
echo getInfo();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment