Skip to content

Instantly share code, notes, and snippets.

@clone1018
Created October 30, 2011 16:39
Show Gist options
  • Save clone1018/1326098 to your computer and use it in GitHub Desktop.
Save clone1018/1326098 to your computer and use it in GitHub Desktop.
Minecraft Ping
<?php
function ping($host, $port=25565, $timeout=30) {
$handle = fsockopen($host, $port, $errno, $errstr, $timeout);
try {
fwrite($handle, "\xFE");
$d = fread($handle, 256);
if ($d[0] != "\xFF") return false;
$d = substr($d, 3);
$d = mb_convert_encoding($d, 'auto', 'UCS-2');
$d = explode("\xA7", $d);
fclose($handle);
} catch(Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
return array('motd' => $d[0], 'players' => (int)$d[1], 'max_players' => (int)$d[2]);
}
var_dump(ping('s.minecrafter.com'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment