Querying Source Servers with PHP : Part 1

Posted on Jan 3, 2017

Over the Christmas Period, I decided to develop a website to monitor the multitude of gaming servers that I host. As a game server provider, I’ve often wondered how many players are actively playing across the network.

For this, I decided to use a simple LEMP (Linux, NGINX, MySQL, PHP) environment. Furthermore, I wanted this website to be heavily reliant on JSON data, and would try to avoid pulling data from MySQL where possible - as I’m assuming pulling from a JSON file (using caching), is faster than from MySQL.

The first task was to obtain a list of the game servers on our network. This was quite easy, due to the fact our billing system already contains this information.

I built a JSON string containing an array of the servers:

Example:

{“1”:{“id”: “1”, “protocol”:“steam”, “game”: “Rust”,“ip”:“46.101.5.78”, “port”: “28015”},“2”:{“id”: “2”, “protocol”:“steam”, “game”: “Rust”,“ip”:“178.62.55.51”, “port”: “28015”}}

Lovely and simple!

ID: Unique Primary Key Protocol: Steam Game: The game! IP: Servers IP Port: Servers Port

 

Now, I have my list of servers. Next it to query them for the data! To do this, we need to interrogate the Source Query Protocol. There’s many open source examples of this out there. I found one by xPaw at https://github.com/xPaw/PHP-Source-Query - It’s very well coded class, and rather than reinventing the wheel, I choose to use his class.

So, I create a cron, to query the server list on a predefined interval.

Here’s the code.

It’s a proof of concept, so don’t judge the coding standards!