Advertisement
masa-

Minecraft query in PHP (simple ping version)

Dec 16th, 2011
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. function ping($host, $port = 25565, $timeout = 1)
  2. {
  3.  
  4.     //Set up our socket
  5.     $fp = fsockopen("tcp://" . $host, $port, $errno, $errstr, $timeout);
  6.  
  7.     if(!$fp)
  8.         return false;
  9.  
  10.     //Send 0xFE: Server list ping
  11.     fwrite($fp, "\xFE");
  12.  
  13.     //Read as much data as we can (max packet size: 241 bytes)
  14.     $d = fread($fp, 256);
  15.  
  16.     //Check we've got a 0xFF Disconnect
  17.     if($d[0] != "\xFF")
  18.         return false;
  19.  
  20.     //Remove the packet ident (0xFF) and the short containing the length of the string
  21.     $d = substr($d, 3);
  22.  
  23.     //Decode UCS-2 string
  24.     $d = mb_convert_encoding($d, 'auto', 'UCS-2');
  25.  
  26.     //Split into array
  27.     $d = explode("\xA7", $d);
  28.  
  29.     //Return an associative array of values
  30.     return array(
  31.             'motd'      => $d[0],
  32.             'players'   => intval($d[1]),
  33.             'max_players'   => intval($d[2])
  34.         );
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement