Skip to content

Instantly share code, notes, and snippets.

@zerotri
Created September 14, 2011 09:33
Show Gist options
  • Save zerotri/1216190 to your computer and use it in GitHub Desktop.
Save zerotri/1216190 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_send( $this->sock, pack( "C*", $string ), $length, 0);
}
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("\xFE");
$response = unpack("C", $socket->recv(1));
if($response[1] != 0xFF)
throw new Exception("Assert failed. Response: ".Print_r($response,true));
$len = unpack("S",$socket->recv(2));
$response = $socket->recv($len[1]*2);
echo "Response: $response";
}
echo getInfo();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment