Last active
July 12, 2020 20:29
-
-
Save conradfr/9263e9473c50d4a6893d467ce0fff688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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