dns_get_record 获取指定主机的DNS记录
发表日期:2021-07-01 08:56:40 | 来源: | | 浏览(1057) 分类:网络 函数
dns_get_record
(PHP 5, PHP 7, PHP 8)
dns_get_record — 获取指定主机的DNS记录
说明
string
$hostname
,int
$type
= DNS_ANY,array
&$authns
= ?,array
&$addtl
= ?,bool
&$raw
= false): array
获取指定主机(hostname
)的DNS记录。
参数
-
hostname
-
主机名(
hostname
)应该是一个DNS解析生效的域名,例如“www.example.com
”。主机名也可以是通过对逆向解析域做DNS逆向域名解析而得到,但是在大多数情况下gethostbyaddr()更加适合做逆向域名解析。注意:
每个DNS标准,邮件地址必须是
user.host
这样的格式(例如hostmaster.example.com
而不是hostmaster@example.com
),在使用mail()这个函数之前请检查这个值,有必要的话还需要修改。 -
type
-
默认情况下,dns_get_record()将会搜索所有与
hostname
相关的记录,可以通过设置type
来限定查询。type
的值可以是下面的其中的任何一个:DNS_A
,DNS_CNAME
,DNS_HINFO
,DNS_MX
,DNS_NS
,DNS_PTR
,DNS_SOA
,DNS_TXT
,DNS_AAAA
,DNS_SRV
,DNS_NAPTR
,DNS_A6
,DNS_ALL
或者DNS_ANY
。注意:
由于dns在各个平台上表现有些不一样,
DNS_ANY
不会总是返回所有的记录,DNS_ALL
虽然慢一些,但是会得到所有的记录,所以使用DNS_ALL更加可靠些。 -
authns
-
以引用方式传递,如果写了该参数,那么将会得到该解析记录的DNS服务器(Authoritative Name Servers)的信息。
-
addtl
-
以引用方式传递,如果填写了该参数,将会得到其他所有的DNS解析记录。
-
raw
-
在原生模式下,在进行额外的查询的时候之前我们只执行请求的DNS类型,而不是循环查询所有的类型。
返回值
这个函数返回一个关联数组,如果失败则 或者在失败时返回 false
。每个关联数组都至少包含了以下的这些键。
at minimum the following keys:
Attribute | Meaning |
---|---|
host | The record in the DNS namespace to which the rest of the associated data refers. |
class |
dns_get_record() only returns Internet class records and as
such this parameter will always return IN .
|
type | String 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 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.
|
Type | Extra Columns |
---|---|
A |
ip : An IPv4 addresses in dotted decimal notation.
|
MX |
pri : Priority of mail exchanger.
Lower numbers indicate greater priority.
target : FQDN of the mail exchanger.
See also 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.
|
SOA |
mname : 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.
|
AAAA |
ipv6 : 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
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.
|
更新日志
版本 | 说明 |
---|---|
5.4.0 |
增加了参数raw 。
|
5.3.0 | 可以是在windows平台上使用这个函数了。 |
5.3.0 |
在此版本之前,如果给参数authns 传入值,则必须同时传入addtl 的值。
|
范例
示例 #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的例子
由于我们经常会想获取一个邮件服务器的对应的IP地址的MX记录是否已经生效。在使用dns_get_record()函数之后,addtl
能够返回一个相关的数组记录,authns
参数则会返回授权服务器的列表信息。
<?php /* Request "ANY" record for php.net, and create $authns and $addtl arrays containing list of name servers and any additional records which go with them */ $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 ) )
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- checkdnsrr 给指定的主机(域名)或者IP地址做DNS通信检查(0)
- closelog 关闭系统日志链接(0)
- dns_check_record 别名 checkdnsrr()(0)
- dns_get_mx 别名 getmxrr()(0)
- dns_get_record 获取指定主机的DNS记录(0)
- fsockopen 打开一个网络连接或者一个Unix套接字连接(0)
- gethostbyaddr 获取指定的IP地址对应的主机名(0)
- gethostbyname 返回主机名对应的 IPv4地址。(0)
- gethostbynamel 获取互联网主机名对应的 IPv4 地址列表(0)
- gethostname 获取主机名(0)
- getmxrr 获取互联网主机名对应的 MX 记录(0)
- getprotobyname Get protocol number associated with protocol name(0)
- getprotobynumber Get protocol name associated with protocol number(0)
- getservbyname 获取互联网服务协议对应的端口(0)
- getservbyport Get Internet service which corresponds to port and protocol(0)
- header_register_callback 调用一个 header 函数(0)
- header_remove 删除之前设置的 HTTP 头(0)
- header 发送原生 HTTP 头(0)
- headers_list 返回已发送的 HTTP 响应头(或准备发送的)(0)
- headers_sent 检测 HTTP 头是否已经发送(0)
- http_response_code 获取/设置响应的 HTTP 状态码(0)
- inet_ntop Converts a packed internet address to a human readable representation(0)
- inet_pton Converts a human readable IP address to its packed in_addr representation(0)
- ip2long 将 IPV4 的字符串互联网协议转换成长整型数字(0)
- long2ip 将长整型转化为字符串形式带点的互联网标准格式地址(IPV4)(0)
- openlog Open connection to system logger(0)
- pfsockopen 打开一个持久的网络连接或者Unix套接字连接。(0)
- setcookie 发送 Cookie(0)
- setrawcookie 发送未经 URL 编码的 cookie(0)
- socket_get_status 别名 stream_get_meta_data()(0)
- socket_set_blocking 别名 stream_set_blocking()(0)
- socket_set_timeout 别名 stream_set_timeout()(0)
- syslog Generate a system log message(0)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)