Advertisement
koki2000

Minecraft server php script

Mar 15th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2. MCServerStatus.php
  3. class MCServerStatus {
  4.  
  5.     public $server;
  6.     public $online, $motd, $online_players, $max_players;
  7.     public $error = "OK";
  8.  
  9.     function __construct($url, $port = '25565')
  10.     {
  11.         $this->server = array(
  12.             "url" => $url,
  13.             "port" => $port
  14.         );
  15.  
  16.         if ( $sock = @stream_socket_client('tcp://'.$url.':'.$port, $errno, $errstr, 1) )
  17.         {
  18.             $this->online = true;
  19.  
  20.             fwrite($sock, "\xfe");
  21.             $h = fread($sock, 2048);
  22.             $h = str_replace("\x00", '', $h);
  23.             $h = substr($h, 2);
  24.             $data = explode("\xa7", $h);
  25.             unset($h);
  26.             fclose($sock);
  27.  
  28.             if (sizeof($data) == 3)
  29.             {
  30.                 $this->motd = $data[0];
  31.                 $this->online_players = (int) $data[1];
  32.                 $this->max_players = (int) $data[2];
  33.             }
  34.             else
  35.             {
  36.                 $this->error = "Cannot retrieve server info.";
  37.             }
  38.         }
  39.         else
  40.         {
  41.             $this->online = false;
  42.             $this->error = "Cannot connect to server.";
  43.         }
  44.     }
  45.  
  46. }
  47. ?>
  48.  
  49. <?php
  50. //index.php
  51. header('Content-Type: text/html; charset=utf-8');
  52. //play.xtrememiners.hu:25565
  53. include "MCServerStatus.php";
  54. $server = new MCServerStatus("play.xtrememiners.hu", 25565); //The second argument is optional in this case
  55.  
  56. $var = $server->online; //$server->online returns true if the server is online, and false otherwise
  57. echo '<pre>';
  58. echo $server->motd; //Outputs the Message of the Day
  59. echo $server->online_players; //Outputs the number of players online
  60. echo $server->max_players; //Outputs the maximum number of players the server allows
  61. print_r($server); //Shows an overview of the object and its contents. (For debugging.)
  62. echo '</pre>';
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement