Git Product home page Git Product logo

minecraft-server-status's Introduction

Lightweight Minecraft server status script for PHP

Copyright noxifoxi https://github.com/noxifoxi/minecraft-server-status

This lightweight script queries the server information from Minecraft java servers.

  • MinecraftServer.php (Minecraft 1.0.0+) uses the query method and supports all servers with enabled queries.
  • MinecraftServerBasic.php (Minecraft Beta 1.8+) can be used as a fallback if the server does not permit queries, if something else goes wrong or just a simple request is required.

Features

  • Queries the server if the server setting enable-query is enabled and parses the server infos.
  • Fallback to read and parse the build-in server infos from Minecraft Beta 1.8+ servers.
  • Lightweight and fast
  • Simple error handling
  • Easy to use

Requirements

  • PHP 7.0+

How to use the script

In order to fetch advanced server information the server's server.properties has to have the following settings:

enable-query=true
query.port=25565

If enable-query is disabled (false) only basic information can be retrieved, methods of MinecraftServerBasic.php will be used then.

This is the suggested method for using this class to fetch server information:

try {
	require_once('MinecraftServer.php');
	$server = new MinecraftServer('example-minecraft-host.com');
} catch (Exception $e) {
	// handle errors
}

If the script throws an Exception it's safe to assume that the server is offline (for you).

Arguments:

MinecraftServer(string $host, int $port = 25565, int $timeout = 1)
  • host - IP or domain
  • port - Port where the Minecraft server is listening (Defaut: 25565)
  • timeout - how long is the script allowed try connecting to the server before it times out in seconds (Default: 1 second)

Examples

Check online/offline

$server->online;

If the server is online the return value is 1 otherwise it's 0.

Get player count

echo $server->numplayers . ' / ' . $server->maxplayers;

Get a list of the online players names

foreach($server->players as $player)
	echo $player . '<br>';

Note: $server->players returns an array of strings.

Get everything

$serverStatus = $server->getInfoArray();

Returns an array with all information the sever sent or the script was able to fetch.

Default server properties

Some of these properties are only available when server query is enabled, properties marked with "(Simple)" are always available via the Simple class or fallback - if the server is online:

  • hostname (Simple)
  • gametype
  • game_id
  • version
  • plugins
  • map
  • numplayers (Simple)
  • maxplayers (Simple)
  • hostport (Simple)
  • hostip (Simple)
  • online (Simple)
  • software

Servers can add more than these default properties, check the keys of Get everything if you are unsure.

Fallback

MinecraftServer.php automatically falls back using MinecraftServerBasic.php if the file is present in the same folder.

minecraft-server-status's People

Contributors

noxifoxi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

minecraft-server-status's Issues

Bug in Zeile12

Ich hab nen Bugg in Zeile 12 :
Parse error: syntax error, unexpected '[' in /var/customers/webs/ni310218_2/MinecraftServerStatus.class.php on line 12

Vom MinecraftServerStatusSimple

Online assertion

$this->Socket = @stream_socket_client('udp://'.$Host.':'.(int)$Port, $ErrNo, $ErrStr, $Timeout);
if($ErrNo || $this->Socket === false)
$this->Info['online'] = false; return;

This is not a complete check to determine if a server is offline. In many circumstances the socket will successfully open regardless of what services are using the port.

The online Info is set to true without any useful checking at the end of this function:
$this->Info['online'] = true;

Should default to false until proven by returned data. e.g. at least one other Info value set.

Parse error: syntax error, unexpected '[' in /home/u337092315/public_html/MinecraftServerStatus.class.php on line 23

I was attempting to use the server status file when I noticed that everywhere I included the file, this error message followed during testing:

Parse error: syntax error, unexpected '[' in /home/u337092315/public_html/MinecraftServerStatus.class.php on line 23

Is this an error on my part? I have been studying the code I have written for days, to no avail. The server I am using currently supports Php 5. Could php versions be a problem (if the script was written in an older version of php)?

the value of online appears to always be 1 when using query

i'm looping through an array of servers to check the status of each and then i build a page. when a server is online the object contains all of the fields the server offers.

when a server is offline the object still contains an empty field with the value NULL, a software field with the value "Vanilla", and an online field with a value of int(1). regardless of the server status when using query it appears the server is always online == 1.

Getting error at line 23 and foreach loop for players

Looks like a bad way of making an array.

error is saying that '[' is not right. I changed to $Info = array(); instead. Works great.

Also, the foreach loop for getting player list is not working correctly. I get...

Warning: Invalid argument supplied for foreach()...

on that line.

'Failed to write on socket' - Minecraft Status Checker

I'm getting this error when trying to use it:

Fatal error: Uncaught exception 'Exception' with message 'Failed to write on socket' in /home/****/****/lib/MinecraftServerStatus.class.php:137 Stack trace: #0 /home/****/****/lib/MinecraftServerStatus.class.php(52): MinecraftServerStatus->Send(9) #1 /home/****/****/index.php(6): MinecraftServerStatus->__construct('mc.****.com') #2 {main} thrown in /home/****/****/lib/MinecraftServerStatus.class.php on line 137

Notice: Undefined offset: 1

I'm using the ip livecraft.es and it returns the information but during connection gives me the following error:

Notice: Undefined offset: 1 in /home/****/public_html/MinecraftServerStatus.class.php on line 93

Also, if I use the ip: server.minecraftgallery.com it will return the information but return an error of:

fwrite(): send of 15 bytes failed with errno=61 Connection refused
Location: MinecraftServerStatus.class.php on line 136
which if i use "error_reporting(0);" turns into "Failed to write on socket"

Thanks!

Throwing exceptions

So far the code is working excellently except for testing purposes I intentionally specified either the wrong port, or shut down my server and instead of $Server->Get('online'); returning false, it throws a "Failed to write on socket" exception. I ended up changing those lines to return false vs throwing an exception. Any ideas?

Players are may not displayed

Script isint working. I get the following error:

Notice: Undefined offset: 1 in W:\XAMPP\htdocs\MinecraftServerStatus.class.php on line 76

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.