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...
(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 :跟此记录关联的任意字符串数据。
|
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 ) )
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...
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);
Get more than one type at once like this:
<?php
$dnsr = dns_get_record('php.net', DNS_A + DNS_NS);
print_r($dnsr);
?>
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:
<?php
$dnsr = dns_get_record('php.net', DNS_ALL - DNS_PTR);
print_r($dnsr);
?>
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.
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.
Sadly this method does not allow for using an arbitrary nameserver.
If you need to make a request using a specific DNS server, you'll need to use either Pear/Net_DNS2, or libdns ( https://github.com/DaveRandom/LibDNS ).
If you want something shorter and lighter, you can also use this 150 lines function: https://gist.github.com/bohwaz/ddc61c4f7e031c3221a89981e70b830c
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
)
)