Advertisement
masa-

Minecraft query in PHP (full version, with player names)

Dec 16th, 2011
2,964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.75 KB | None | 0 0
  1. function full_stat($host, $port = 25565, $timeout = 1)
  2. {
  3.     //Set up our socket
  4.     $fp = fsockopen("udp://" . $host, $port, $errno, $errstr, $timeout);
  5.  
  6.     if(!$fp)
  7.         return false;
  8.  
  9.     // Get the challenge token; send 0xFE 0xFD 0x09 and a 4-byte session id
  10.     $str1 = "\xFE\xFD\x09\x01\x02\x03\x04"; // Arbitrary session id at the end (4 bytes)
  11.     fwrite($fp, $str1);
  12.     $resp1 = fread($fp, 256);
  13.  
  14.     if($resp1[0] != "\x09") // Check for a valid response
  15.         return false;
  16.  
  17.     // Parse the challenge token from string to integer
  18.     $token = 0;
  19.     for($i = 5; $i < (strlen($resp1) - 1); $i++)
  20.     {
  21.         $token *= 10;
  22.         $token += $resp1[$i];
  23.     }
  24.  
  25.     // Divide the int32 into 4 bytes
  26.     $token_arr = array( 0 => ($token / (256*256*256)) % 256,
  27.                 1 => ($token / (256*256)) % 256,
  28.                 2 => ($token / 256) % 256,
  29.                 3 => ($token % 256)
  30.             );
  31. /*
  32.     // Get the short version of server status. ID and challenge tokens are appended to the command 0x00.
  33.     $str = "\xFE\xFD\x00\x01\x02\x03\x04"
  34.         . chr($token_arr[0]) . chr($token_arr[1]) . chr($token_arr[2]) . chr($token_arr[3]);
  35.     fwrite($fp, $str);
  36.     $data1 = fread($fp, 4096);
  37.     $short_stat = substr($data1, 5);    // Strip the crap from the start
  38.  
  39.     $short_stat = explode("\x00", $short_stat); // Explode the payload from the NULL-terminated string ends
  40.     $port = ord($short_stat[5][1]) * 256 + ord($short_stat[5][0]);  // Big-endian short
  41.  
  42.     $short_stat = array(    'motd'      => $short_stat[0],
  43.                 'gametype'  => $short_stat[1],
  44.                 'map'       => $short_stat[2],
  45.                 'numplayers'    => $short_stat[3],
  46.                 'maxplayers'    => $short_stat[4],
  47.                 'portnum'   => $port,
  48.                 'hostname'  => substr($short_stat[5], 2)    // Host IP after the port number
  49.             );
  50. */
  51.  
  52.     // Get the full version of server status. ID and challenge tokens appended to command 0x00, payload padded to 8 bytes.
  53.     $str = "\xFE\xFD\x00\x01\x02\x03\x04"
  54.         . chr($token_arr[0]) . chr($token_arr[1]) . chr($token_arr[2]) . chr($token_arr[3])
  55.         . "\x00\x00\x00\x00";
  56.     fwrite($fp, $str);
  57.     $data2 = fread($fp, 4096);
  58.     $full_stat = substr($data2, 11);    // Strip the crap from the start
  59.  
  60.     $tmp = explode("\x00\x01player_\x00\x00", $full_stat);  // First, split the payload in two parts
  61.     $t = explode("\x00", $tmp[0]);      // Divide the first part from every NULL-terminated string end into key1 val1 key2 val2...
  62.     unset($t[count($t) - 1]);       // Unset the last entry, because the are two 0x00 bytes at the end
  63.     $t2 = explode("\x00", $tmp[1]);     // Explode the player information from the second part
  64.  
  65.     $info = array();
  66.     for($i = 0; $i < count($t); $i += 2)
  67.     {
  68.         if($t[$i] == "")
  69.             break;
  70.  
  71.         $info[$t[$i]] = $t[$i + 1];
  72.     }
  73.  
  74.     $players = array();
  75.     foreach($t2 as $one)
  76.     {
  77.         if($one == "")
  78.             break;
  79.  
  80.         $players[] = $one;
  81.     }
  82.  
  83.     $full_stat = $info;
  84.     $full_stat['players'] = $players;
  85.  
  86.     return $full_stat;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement