Skip to content

Instantly share code, notes, and snippets.

@pdelvo
Forked from barneygale/gist:1235274
Created October 18, 2012 20:40
Show Gist options
  • Save pdelvo/3914563 to your computer and use it in GitHub Desktop.
Save pdelvo/3914563 to your computer and use it in GitHub Desktop.
<?php
function ping($host, $port=25565, $timeout=30) {
//Set up our socket
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) return false;
//Send 0xFE: Server list ping
fwrite($fp, "\xFE");
fwrite($fp, "\x00");
//Read as much data as we can (max packet size: 241 bytes)
$d = fread($fp, 256);
//Check we've got a 0xFF Disconnect
if ($d[0] != "\xFF") return false;
//Remove the packet ident (0xFF) and the short containing the length of the string
$d = substr($d, 3);
//Decode UCS-2 string
$d = mb_convert_encoding($d, 'auto', 'UCS-2');
//Split into array
$d = explode("\x00", $d);
//Return an associative array of values
return array(
'protocol_version': intval($d[1]),
'minecraft_version': $d[2],
'motd' => $d[3],
'players' => intval($d[4]),
'max_players' => intval($d[5]));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment