muffinjello

Minecraft Query Code

May 25th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2. /*
  3.      * Minecraft Server Status Query
  4.      * Code by Julian Spravil <julian.spr@t-online.de>
  5.      * GitHub: https://github.com/FunnyItsElmo/PHP-Minecraft-Server-Status-Query
  6.     */
  7.     /**
  8.      * Minecraft Server Status Query
  9.      * @author Julian Spravil <julian.spr@t-online.de> https://github.com/FunnyItsElmo
  10.      * @license Free to use but dont remove the author, license and copyright
  11.      * @copyright © 2013 Julian Spravil
  12.      */
  13.     class MinecraftServerStatus {
  14.         private $timeout;
  15.  
  16.         /**
  17.          * Prepares the class.
  18.          * @param int    $timeout   default(3)
  19.          */
  20.         public function __construct($timeout = 3) {
  21.             $this->timeout = $timeout;
  22.         }
  23.  
  24.         /**
  25.          * Gets the status of the target server.
  26.          * @param string    $host    domain or ip address
  27.          * @param int    $port    default(25565)
  28.          */
  29.         public function getStatus($host = 's6.hosthorde.com', $port = 25575) {
  30.  
  31.             //Transform domain to ip address.
  32.             if (substr_count($host , '.') != 4) $host = gethostbyname($host);
  33.  
  34.             //Get timestamp for the ping
  35.             $start = microtime(true);
  36.  
  37.             //Connect to the server
  38.             if(!$socket = @stream_socket_client('tcp://'.$host.':'.$port, $errno, $errstr, $this->timeout)) {
  39.  
  40.                 //Server is offline
  41.                 return false;
  42.  
  43.  
  44.             } else {
  45.  
  46.                 stream_set_timeout($socket, $this->timeout);
  47.  
  48.                 //Write and read data
  49.                 fwrite($socket, "\xFE");
  50.                 $data = fread($socket, 2048);
  51.                 fclose($socket);
  52.                 if($data == null) return false;
  53.  
  54.                 //Calculate the ping
  55.                 $ping = round((microtime(true)-$start)*1000);
  56.  
  57.                 //Evaluate the received data
  58.                 if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){
  59.  
  60.                     $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
  61.                     $motd = preg_replace("/(§.)/", "",$result[1]);
  62.  
  63.                 }else{
  64.  
  65.                     $result = explode('§', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
  66.  
  67.                     $motd = "";
  68.                     foreach ($result as $key => $string) {
  69.                         if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $key != 0) {
  70.                             $motd .= '§'.$string;
  71.                         }
  72.                     }
  73.  
  74.                     $motd = preg_replace("/(§.)/", "", $motd);
  75.  
  76.                 }
  77.  
  78.                 //Set variables
  79.                 $res = array();
  80.                 $res['hostname'] = $host;
  81.                 $res['version'] = $result[0];
  82.                 $res['motd'] = $motd;
  83.                 $res['players'] = $result[sizeof($result)-2];
  84.                 $res['maxplayers'] = $result[sizeof($result)-1];
  85.                 $res['ping'] = $ping;
  86.  
  87.                 //return obj
  88.                 return $res;
  89.             }
  90.  
  91.         }
  92.     }
  93. $status = new MinecraftServerStatus(); // call the class
  94. $response = $status->getStatus('s6.hosthorde.com', 25575); // when you dont have the default port
  95. if(!$response){ return"<span class='label red'>Offline</span>"; } else { return"<span class='label green'>Online</span>"; }
Add Comment
Please, Sign In to add comment