PHP查询SRV记录

我运行一个Minecraft网站,目前在使用查询协议时,它无法与SRV记录一起使用.

我只是想知道有没有办法获得SRV记录所指向的ip和端口.

E.g: mc.lunarphase.co.uk => 192.198.91.238:64759

解决方法:

最简单的方法可能是使用挖掘.你可以直接使用套接字,但这样的东西更容易(恕我直言):

function getDNS($hostname, $type='') {
        $records=`dig +noall +answer $hostname $type`;
        $records=explode("\n",$records);
        $res_hostname='';
        $port=0;

        foreach ($records as $record) {
                preg_match_all('/([^\s]+)\s*/',$record, $matches);
                if (is_array($matches) && is_array($matches[1]) && count($matches[1]) > 3) {
                        switch (strtoupper($matches[1][3])) {
                        case 'SRV':
                                if (count($matches[1]) > 6) {
                                        $port=$matches[1][6];
                                        $res_hostname=$matches[1][7];
                                }
                                break;
                        case 'A':
                        case 'CNAME':
                                if (count($matches[1]) > 3) {
                                        $res_hostname=$matches[1][4];
                                }
                                break;
                        }
                        if (!empty($res_hostname) && substr($res_hostname, -1) == '.') break; // if it's a cname, we've probably already got the ip, so keep going just in case (but we might not so don't count on it!)
                }
        }
        if (substr($res_hostname, -1) == '.') { // we have more resolving to do
                $res_hostname=getDNS(trim($res_hostname, '. '));
        }

        if (empty($res_hostname)) die('Failed to lookup IP for ' . (!empty($type) ? '(' . $type .' record) ' : '' ) . $hostname . PHP_EOL);
        if (empty($port)) return $res_hostname;
        return $res_hostname . ':' . $port;
}
$hostIPPair=getDNS('example.com', 'srv');
上一篇:C#Ping我的世界


下一篇:寓教于乐的功能游戏——Minecraft