Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- MCServerStatus.php
- class MCServerStatus {
- public $server;
- public $online, $motd, $online_players, $max_players;
- public $error = "OK";
- function __construct($url, $port = '25565')
- {
- $this->server = array(
- "url" => $url,
- "port" => $port
- );
- if ( $sock = @stream_socket_client('tcp://'.$url.':'.$port, $errno, $errstr, 1) )
- {
- $this->online = true;
- fwrite($sock, "\xfe");
- $h = fread($sock, 2048);
- $h = str_replace("\x00", '', $h);
- $h = substr($h, 2);
- $data = explode("\xa7", $h);
- unset($h);
- fclose($sock);
- if (sizeof($data) == 3)
- {
- $this->motd = $data[0];
- $this->online_players = (int) $data[1];
- $this->max_players = (int) $data[2];
- }
- else
- {
- $this->error = "Cannot retrieve server info.";
- }
- }
- else
- {
- $this->online = false;
- $this->error = "Cannot connect to server.";
- }
- }
- }
- ?>
- <?php
- //index.php
- header('Content-Type: text/html; charset=utf-8');
- //play.xtrememiners.hu:25565
- include "MCServerStatus.php";
- $server = new MCServerStatus("play.xtrememiners.hu", 25565); //The second argument is optional in this case
- $var = $server->online; //$server->online returns true if the server is online, and false otherwise
- echo '<pre>';
- echo $server->motd; //Outputs the Message of the Day
- echo $server->online_players; //Outputs the number of players online
- echo $server->max_players; //Outputs the maximum number of players the server allows
- print_r($server); //Shows an overview of the object and its contents. (For debugging.)
- echo '</pre>';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement