Guest User

Untitled

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