百科狗-知识改变命运!
--

dns_get_record() - php 网络函数

梵高1年前 (2023-11-21)阅读数 17#技术干货
文章标签有的

dns_get_record()

(PHP 5, PHP 7)

获取指定主机的DNS记录

说明

dns_get_record(string $hostname[,int $type= DNS_ANY[,array &$authns[,array &$addtl[,bool &$raw= false]]]]): array

获取指定主机($hostname)的DNS记录。

参数

$hostname

主机名($hostname)应该是一个DNS解析生效的域名,例如“www.example.com”。主机名也可以是通过对逆向解析域做DNS逆向域名解析而得到,但是在大多数情况下gethostbyaddr()更加适合做逆向域名解析。

Note:

每个DNS标准,邮件地址必须是user.host这样的格式(例如hostmaster.example.com而不是hostmaster@example.com),在使用mail()这个函数之前请检查这个值,有必要的话还需要修改。$type

默认情况下,dns_get_record()将会搜索所有与$hostname相关的记录,可以通过设置$type来限定查询。$type的值可以是下面的其中的任何一个:DNS_ADNS_CNAMEDNS_HINFODNS_MXDNS_NSDNS_PTRDNS_SOADNS_TXTDNS_AAAADNS_SRVDNS_NAPTRDNS_A6DNS_ALL或者DNS_ANY

Note:

由于dns在各个平台上表现有些不一样,DNS_ANY不会总是返回所有的记录,DNS_ALL虽然慢一些,但是会得到所有的记录,所以使用DNS_ALL更加可靠些。$authns

以引用方式传递,如果写了该参数,那么将会得到该解析记录的DNS服务器(Authoritative Name Servers)的信息。

$addtl

以引用方式传递,如果填写了该参数,将会得到其他所有的DNS解析记录

$raw

在原生模式下,在进行额外的查询的时候之前我们只执行请求的DNS类型,而不是循环查询所有的类型。

返回值

这个函数返回一个关联数组,如果失败则或者在失败时返回FALSE。每个关联数组都至少包含了以下的这些键。at minimumthe following keys:

Basic DNS attributes
AttributeMeaning
hostThe record in the DNS namespace to which the rest of the associated data refers.
classdns_get_record()only returns Internet class records and as such this parameter will always returnIN.
typeString containing the record type. Additional attributes will also be contained in the resulting array dependant on the value of type. See table below.
ttl"Time To Live"remaining for this record. This willnotequal the record's original ttl, but will rather equal the original ttl minus whatever length of time has passed since the authoritative name server was queried.
Other keys in associative arrays dependant on 'type'
TypeExtra Columns
Aip: An IPv4 addresses in dotted decimal notation.
MXpri: Priority of mail exchanger. Lower numbers indicate greater priority.target: FQDN of the mail exchanger. See alsodns_get_mx().
CNAMEtarget: FQDN of location in DNS namespace to which the record is aliased.
NStarget: FQDN of the name server which is authoritative for this hostname.
PTRtarget: Location within the DNS namespace to which this record points.
TXTtxt: Arbitrary string data associated with this record.
HINFOcpu: IANA number designating the CPU of the machine referenced by this record.os: IANA number designating the Operating System on the machine referenced by this record. See IANA's»Operating System Namesfor the meaning of these values.
SOAmname: FQDN of the machine from which the resource records originated.rname: Email address of the administrative contain for this domain.serial: Serial # of this revision of the requested domain.refresh: Refresh interval (seconds) secondary name servers should use when updating remote copies of this domain.retry: Length of time (seconds) to wait after a failed refresh before making a second attempt.expire: Maximum length of time (seconds) a secondary DNS server should retain remote copies of the zone data without a successful refresh before discarding.minimum-ttl: Minimum length of time (seconds) a client can continue to use a DNS resolution before it should request a new resolution from the server. Can be overridden by individual resource records.
AAAAipv6: IPv6 address
A6(PHP >= 5.1.0)masklen: Length (in bits) to inherit from the target specified by$chain.ipv6: Address for this specific record to merge with$chain.chain: Parent record to merge with$ipv6data.
SRVpri:(Priority) lowest priorities should be used first.weight: Ranking to weight which of commonly prioritized$targetsshould be chosen at random.targetandport: hostname and port where the requested service can be found. For additional information see:» RFC 2782
NAPTRorderandpref: Equivalent to$priand$weightabove.flags,services,regex,andreplacement: Parameters as defined by» RFC 2915.

更新日志

版本说明
5.4.0增加了参数$raw。
5.3.0可以是在windows平台上使用这个函数了。
5.3.0在此版本之前,如果给参数$authns传入值,则必须同时传入$addtl的值。

范例

dns_get_record() - php 网络函数

使用dns_get_record()函数

以上例程的输出类似于:

Array
(
    [0] => Array
        (
            [host] => php.net
            [type] => MX
            [pri] => 5
            [target] => pair2.php.net
            [class] => IN
            [ttl] => 6765
        )
    [1] => Array
        (
            [host] => php.net
            [type] => A
            [ip] => 64.246.30.37
            [class] => IN
            [ttl] => 8125
        )
)

使用dns_get_record()配合DNS_ANY的例子

由于我们经常会想获取一个邮件服务器的对应的IP地址的MX记录是否已经生效。在使用dns_get_record()函数之后,$addtl能够返回一个相关的数组记录,$authns参数则会返回授权服务器的列表信息。

以上例程的输出类似于:

Result = Array
(
    [0] => Array
        (
            [host] => php.net
            [type] => MX
            [pri] => 5
            [target] => pair2.php.net
            [class] => IN
            [ttl] => 6765
        )
    [1] => Array
        (
            [host] => php.net
            [type] => A
            [ip] => 64.246.30.37
            [class] => IN
            [ttl] => 8125
        )
)
Auth NS = Array
(
    [0] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => remote1.easydns.com
            [class] => IN
            [ttl] => 10722
        )
    [1] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => remote2.easydns.com
            [class] => IN
            [ttl] => 10722
        )
    [2] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => ns1.easydns.com
            [class] => IN
            [ttl] => 10722
        )
    [3] => Array
        (
            [host] => php.net
            [type] => NS
            [target] => ns2.easydns.com
            [class] => IN
            [ttl] => 10722
        )
)
Additional = Array
(
    [0] => Array
        (
            [host] => pair2.php.net
            [type] => A
            [ip] => 216.92.131.5
            [class] => IN
            [ttl] => 6766
        )
    [1] => Array
        (
            [host] => remote1.easydns.com
            [type] => A
            [ip] => 64.39.29.212
            [class] => IN
            [ttl] => 100384
        )
    [2] => Array
        (
            [host] => remote2.easydns.com
            [type] => A
            [ip] => 212.100.224.80
            [class] => IN
            [ttl] => 81241
        )
    [3] => Array
        (
            [host] => ns1.easydns.com
            [type] => A
            [ip] => 216.220.40.243
            [class] => IN
            [ttl] => 81241
        )
    [4] => Array
        (
            [host] => ns2.easydns.com
            [type] => A
            [ip] => 216.220.40.244
            [class] => IN
            [ttl] => 81241
        )
)

注释

Note:

For compatibility with versions before PHP 5.3.0 on some operating systems, try the» PEARclass» Net_DNS.

参见

  • dns_get_mx() 别名 getmxrr
  • dns_check_record() 别名 checkdnsrr
This method has no error handling, it simply puts out "false" and it is impossible to check for NXDOMAIN, SERVFAIL, TIMEOUT or any other error...
Get more than one type at once like this:

Using DNS_ALL fails on some domains where DNS_ANY works. I noticed the function getting stuck on the DNS_PTR record, which caused it to return FALSE with this error:
PHP Warning: dns_get_record(): res_nsend() failed in ....
This gets all records except DNS_PTR: 
Although this works very well for general DNS queries if you want to do a direct DNS query to a specified DNS server (rather than using OS resolution) try PHPDNS: http://www.purplepixie.org/phpdns/
You can do direct (TCP or UDP) low-level queries to a nameserver and recurse at will. Very useful for testing specific servers and also for walking through a recursive resolution.
You might have the same problem as me, where testing a non-existent domain will search for a subdomain relative to the domain you are executing from, for example:
// Test with working domain
var_dump( dns_get_record('google.com', DNS_A) );
/* works, returns
Array
(
  [host] => google.com
  [class] => IN
  [ttl] => 299
  [type] => A
  [ip] => 172.217.12.142
)
*/
// Test with invalid domain on our website (example.com)
var_dump( dns_get_record('invalidtestingname.com', DNS_A) );
/* Doesn't work, pretend it's a subdomain
Array
(
  [host] => invalidtestingname.com.example.com
  [class] => IN
  [ttl] => 299
  [type] => A
  [ip] => xxx.xxx.xxx.xxx
)
*/
If anyone has that problem, add a "dot" at the end of the domain name, for example, instead of
dns_get_record('invalidtestingname.com', DNS_A);
Do this:
dns_get_record('invalidtestingname.com.', DNS_A);
Please note that Firewalls and anti malware software detects (and depending on company policies even blocks) DNS_ANY requests. 
In that case the usage of this function fails.
This is because DNS_ANY requests can be exploited for creating "amplification (D)DOS attackes": You send 1 DNS_ANY request and get a huge amount of information back, thus even small requests can result into hugh network load.
I advise to use a more explicit name-request instead of using DNS_ANY.
When I use DNS_ALL as the second parameter to invoke dns_get_record() on the OS of Windows, PHP emits a warning with the message "Warning: dns_get_record(): Type '251721779' not supported in blah.php on line blah", and DNS_ANY is always OKAY.
Hi,
If I added domain to directadmin where is script with dns_get_record it gives me local values even if that domain is not really delegated yet to this server.. why?
There's a comment below from 7 years ago regarding BSD and MacOSX, I'd just like to follow up incase some people see that and don't think it'll work on MacOSX.
Software:
  System Software Overview:
   System Version: OS X 10.8.3 (12D78)
   Kernel Version: Darwin 12.3.0
   Boot Volume: Macintosh HD
   Boot Mode: Normal
   Computer Name: Karl’s iMac
   User Name: Karl Kloppenborg (karl)
   Secure Virtual Memory: Enabled
   Time since boot: 10 days 20:24
--------------
# php -a
php > print_r(dns_get_record('google.com', DNS_MX));
Array
(
  [0] => Array
    (
      [host] => google.com
      [type] => MX
      [pri] => 10
      [target] => aspmx.l.google.com
      [class] => IN
      [ttl] => 749
    )
  [1] => Array
    (
      [host] => google.com
      [type] => MX
      [pri] => 30
      [target] => alt2.aspmx.l.google.com
      [class] => IN
      [ttl] => 749
    )
  [2] => Array
    (
      [host] => google.com
      [type] => MX
      [pri] => 50
      [target] => alt4.aspmx.l.google.com
      [class] => IN
      [ttl] => 749
    )
  [3] => Array
    (
      [host] => google.com
      [type] => MX
      [pri] => 40
      [target] => alt3.aspmx.l.google.com
      [class] => IN
      [ttl] => 749
    )
  [4] => Array
    (
      [host] => google.com
      [type] => MX
      [pri] => 20
      [target] => alt1.aspmx.l.google.com
      [class] => IN
      [ttl] => 749
    )
)

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)