M
M
Mark Eshkilev2021-02-18 22:33:37
PHP
Mark Eshkilev, 2021-02-18 22:33:37

How to make GMOD server monitoring in php?

Hello, how can I make Gmod server monitoring in php so that it displays how many players are on the server, is the server turned on, and what map?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery Chmykh, 2021-02-19
@mark_edinoroglove

Example

<?php

function getSourceServerInfo(string $ip, string $port): array
{
    $info = [
        'status' => 0,
        'ip' => $ip,
        'port' => $port
    ];

    $socket = @fsockopen("udp://{$ip}", $port, $errno, $errstr, 1);

    if (!$socket) {
        return $info;
    }

    stream_set_timeout($socket, 1);
    stream_set_blocking($socket, true);

    fwrite($socket, "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00");
    $packet = fread($socket, 4096);
    @fclose($socket);

    if (!$packet) {
        return $info;
    }

    if (substr($packet, 4, 1) != "I") {
        exit("NOT A SOURCE SERVER");
    }

    $packet_array = explode("\x00", substr($packet, 6), 5);
    $info['name'] = $packet_array[0];
    $info['map'] = $packet_array[1];
    $info['game'] = $packet_array[2];
    $info['description'] = $packet_array[3];
    $packet = $packet_array[4];
    $info['players'] = ord(substr($packet, 2, 1));
    $info['playersmax'] = ord(substr($packet, 3, 1));
    $info['bots'] = ord(substr($packet, 4, 1));
    $info['status'] = 1;
    $info['vac'] = ord(substr($packet, 8, 1));

    return $info;
}


$query = getSourceServerInfo("51.89.142.229", '27016');
echo '<pre>';
echo print_r($query, 1);
echo '</pre>';

/*
Array
(
    [status] => 1
    [ip] => 51.89.142.229
    [port] => 27016
    [name] => PuschiRP [Bitmining] [Printer] [Custom]
    [map] => rp_downtown_tits_v2
    [game] => garrysmod
    [description] => DarkRP
    [players] => 12
    [playersmax] => 32
    [bots] => 0
    [vac] => 1
)
*/

S
Sergey Romanenko, 2021-02-18
@Awilum

it's probably necessary to write this here https://freelance.habr.com/tasks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question