FFY00

MC Satus

Oct 13th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author Kristaps Karlsons <[email protected]>
  4.  * Licensed under MPL 1.1
  5.  */
  6.  
  7. function mc_status($host,$port='25565') {
  8.     $timeInit = microtime();
  9.     // TODO: implement a way to store data (memcached or MySQL?) - please don't overload target server
  10.     $fp = fsockopen($host,$port,$errno,$errstr,$timeout=10);
  11.     if(!$fp) die($errstr.$errno);
  12.     else {
  13.         fputs($fp, "\xFE"); // xFE - get information about server
  14.         $response = '';
  15.  
  16.         while(!feof($fp)) $response .= fgets($fp);
  17.         fclose($fp);
  18.         $timeEnd = microtime();
  19.  
  20.         $response = str_replace("\x00", "", $response); // remove NULL
  21.  
  22.         //$response = explode("\xFF", $response); // xFF - data start (old version, prior to 1.0?)
  23.         $response = explode("\xFF\x16", $response); // data start
  24.  
  25.         $response = $response[1]; // chop off all before xFF (could be done with regex actually)
  26.  
  27.         //echo(dechex(ord($response[0])));
  28.         $response = explode("\xA7", $response); // xA7 - delimiter
  29.  
  30.         $timeDiff = $timeEnd-$timeInit;
  31.         $response[] = $timeDiff < 0 ? 0 : $timeDiff;
  32.     }
  33.     return $response;
  34. }
  35.  
  36. $data = mc_status('mc.exs.lv','25592'); // even better - don't use hostname but provide IP instead (DNS lookup is a waste)
  37.  
  38. print_r($data); // [0] - motd, [1] - online, [2] - slots, [3] - time of request (in microseconds - use this to present latency information)
Advertisement
Add Comment
Please, Sign In to add comment