首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP查询SRV记录

PHP查询SRV记录
EN

Stack Overflow用户
提问于 2013-07-02 04:07:05
回答 2查看 2K关注 0票数 3

我运行的是一个“我的世界”网站,目前在使用查询协议时,它不能处理SRV记录。

我想知道是否有一种方法可以获得SRV记录所指向的ip和端口。

代码语言:javascript
复制
E.g: mc.lunarphase.co.uk => 192.198.91.238:64759
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-02 05:26:23

最简单的方法可能是使用dig。您可以直接使用套接字,但这样做更简单(IMHO):

代码语言:javascript
复制
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');
票数 2
EN

Stack Overflow用户

发布于 2016-01-15 17:15:48

你可以使用dns_get_record。

代码语言:javascript
复制
$result = dns_get_record("_http._tcp.mxtoolbox.com", DNS_SRV);
print_r($result);

输出结果如下:

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [host] => _http._tcp.mxtoolbox.com
            [class] => IN
            [ttl] => 2409
            [type] => SRV
            [pri] => 10
            [weight] => 100
            [port] => 80
            [target] => mxtoolbox.com
        )

)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17412765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档