(PHP 5, PHP 7, PHP 8)
dns_get_record — 获取指定主机名的 DNS 纪录
$hostname
,$type
= DNS_ANY
,&$authoritative_name_servers
= null
,&$additional_records
= null
,$raw
= false
获取指定 hostname
的 DNS 纪录。
hostname
hostname
应该是有效的 DNS 主机名,比如“www.example.com
”。可以使用
in-addr.arpa
表示法生成反向查找,但大多数情况下更适合用 gethostbyaddr()。
注意:
根据 DNS 标准,邮件地址以
user.host
格式给出(例如:hostmaster.example.com
而不是hostmaster@example.com
),请务必检查此值并在必要时进行修改,然后将其与 mail() 等函数一起使用。
type
默认情况下,dns_get_record() 将会搜索跟 hostname
关联的任何资源记录。要限制查询,可以指定可选的 type
选项。可以是以下之一:DNS_A
、DNS_CNAME
、DNS_HINFO
, DNS_CAA
、DNS_MX
、DNS_NS
、DNS_PTR
、DNS_SOA
、DNS_TXT
、DNS_AAAA
、DNS_SRV
、DNS_NAPTR
、DNS_A6
、DNS_ALL
或 DNS_ANY
。
注意:
因为不同平台间的 libresolv 存在性能差异,
DNS_ANY
不会始终返回每条记录,较慢的DNS_ALL
会更可靠的收集所有记录。
注意:
Windows:不支持
DNS_CAA
。没有实现对DNS_A6
的支持。
authoritative_name_servers
引用传递,如果给出,将会填充权威名称服务器的资源记录。
additional_records
引用传递,如果给出,将会填充任何附加记录。
raw
type
将解释为原始 DNS 类型 ID(不能使用 DNS_*
常量)。返回值包含 data
键,需要手动解析。
此函数返回由关联数组组成的数组, 或者在失败时返回 false
。每个关联数组至少包含以下键:
属性 | 含义 |
---|---|
host | The record in the DNS namespace to which the rest of the associated data refers. |
class |
dns_get_record() 仅返回内部类记录,因此参数将始终返回 IN 。
|
type | 包含记录类型的字符串。其他属性也包含在结果数组中,具体取决于 type 的值。查看下表。 |
ttl |
"Time To Live" remaining for this record. This will not equal
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.
|
类型 | 额外列 |
---|---|
A |
ip :点分十进制格式的 IPv4。
|
MX |
pri :邮件交换器的有优先级。较低的数字有较高的优先级。target :邮件服务器的
FQDN(全称域名)。参阅 dns_get_mx()。
|
CNAME |
target : FQDN of location in DNS namespace to which
the record is aliased.
|
NS |
target : FQDN of the name server which is authoritative
for this hostname.
|
PTR |
target : Location within the DNS namespace to which
this record points.
|
TXT |
txt : Arbitrary string data associated with this record.
|
HINFO |
cpu : 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
Names for the meaning of these values.
|
CAA |
flags :单字节位字段;目前仅定义了第 0
位,意味着“critical”;其他位保留且应该忽略。tag :CAA
标记名(字母数字的 ASCII 字符串)。value :CAA
标记值(二进制字符串,可以使用子格式)。更多信息参阅:» RFC 6844。
|
SOA |
mname : FQDN of the machine from which the resource
records originated.
rname : Email address of the administrative contact
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.
|
AAAA |
ipv6 :IPv6 地址
|
A6 |
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
ipv6 data.
|
SRV |
pri : (Priority) lowest priorities should be used first.
weight : Ranking to weight which of commonly prioritized
targets should be chosen at random.
target and port : hostname and port
where the requested service can be found.
For additional information see: » RFC 2782
|
NAPTR |
order and pref : Equivalent to
pri and weight above.
flags , services , regex ,
and replacement : Parameters as defined by
» RFC 2915.
|
版本 | 说明 |
---|---|
7.0.16, 7.1.2 | 新增对 CAA 记录的支持。 |
示例 #1 使用 dns_get_record()
<?php
$result = dns_get_record("php.net");
print_r($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 ) )
示例 #2 使用 dns_get_record() 和 DNS_ANY
一旦解析了 MX 记录,通常需要邮件服务器的 IP 地址,因此 dns_get_record()
还会在 additional_records
中返回包含关联记录的数组。authoritative_name_servers
也会返回,包含权威名称服务器列表。
<?php
/* 为 php.net 请求“ANY”记录,并创建 $authns 和 $addtl 数组,
包含名称服务器列表和任何附加记录列表 */
$result = dns_get_record("php.net", DNS_ANY, $authns, $addtl);
echo "Result = ";
print_r($result);
echo "Auth NS = ";
print_r($authns);
echo "Additional = ";
print_r($addtl);
?>
以上例程的输出类似于:
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 ) )