Skip to content

Instantly share code, notes, and snippets.

@conradfr
Last active July 12, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conradfr/9263e9473c50d4a6893d467ce0fff688 to your computer and use it in GitHub Desktop.
Save conradfr/9263e9473c50d4a6893d467ce0fff688 to your computer and use it in GitHub Desktop.
<?php
const SERVERS_DNS = 'all.api.radio-browser.info';
function getServers(): array
{
$serversFromDns = dns_get_record(SERVERS_DNS, DNS_ANY);
$serversAll = array_reduce($serversFromDns, function ($result, $record) {
$ip = isset($record['ip']) ? $record['ip'] : $record['ipv6'];
$host = gethostbyaddr($ip);
if ($host !== false) {
$result[] = $host;
}
return $result;
}, []);
return array_unique($serversAll);
}
function getOneRandomServer(): ?string
{
$servers = getServers();
if (count($servers) === 0) { return null; }
$picked = mt_rand(0, count($servers) - 1);
return $servers[$picked];
}
$server = getOneRandomServer();
echo 'Server picked: ' . $server;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment