哈希空间 Ctrl + F 进行搜索
首页 php手册中文版 CSS中文手册 哈希文档 Markdown在线工具

PHP dns 组件

dns_get_record

(PHP 5, PHP 7, PHP 8)

dns_get_record获取指定主机名的 DNS 纪录

说明

dns_get_record(
    string $hostname,
    int $type = DNS_ANY,
    array &$authoritative_name_servers = null,
    array &$additional_records = null,
    bool $raw = false
): array|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_ADNS_CNAMEDNS_HINFO, DNS_CAADNS_MXDNS_NSDNS_PTRDNS_SOADNS_TXTDNS_AAAADNS_SRVDNS_NAPTRDNS_A6DNS_ALLDNS_ANY

注意:

因为不同平台间的 libresolv 存在性能差异,DNS_ANY 不会始终返回每条记录,较慢的 DNS_ALL 会更可靠的收集所有记录。

注意:

Windows:不支持 DNS_CAA。没有实现对 DNS_A6 的支持。

authoritative_name_servers

引用传递,如果给出,将会填充权威名称服务器的资源记录。

additional_records

引用传递,如果给出,将会填充任何附加记录

raw

type 将解释为原始 DNS 类型 ID(不能使用 DNS_* 常量)。返回值包含 data 键,需要手动解析。

返回值

此函数返回由关联数组组成的数组, 或者在失败时返回 false。每个关联数组至少包含以下键:

基础 DNS 属性
属性 含义
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.

根据“type”的不同,出现的关联数组的其它键
类型 额外列
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
        )

)

参见

打开 哈希空间 微信小程序中查看更佳