Created
October 30, 2011 16:39
-
-
Save clone1018/1326098 to your computer and use it in GitHub Desktop.
Minecraft Ping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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