<?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
)
*/