📅  最后修改于: 2023-12-03 15:18:22.171000             🧑  作者: Mango
PHP的dns_get_record()
函数用于获取域名的DNS记录。它可以返回该域名的所有A记录、MX记录、NS记录、SOA记录、TXT记录等。
该函数具有以下参数:
$records = dns_get_record("example.com");
print_r($records);
该代码将返回一组DNS记录数组,因为没有指定类型,它将返回DNS_ANY的所有记录。例如:
Array
(
[0] => Array
(
[host] => example.com
[class] => IN
[ttl] => 3599
[type] => MX
[pri] => 10
[target] => mx01.1and1.com
)
[1] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => A
[ip] => 216.58.194.174
)
)
可以使用第二个参数来指定要查询的记录类型,例如,要获取特定域名的所有NS记录,可以使用以下代码:
$records = dns_get_record("example.com", DNS_NS);
print_r($records);
该代码将返回以下数组:
Array
(
[0] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns1.p03.dynect.net
)
[1] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns2.p03.dynect.net
)
[2] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns3.p03.dynect.net
)
[3] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns4.p03.dynect.net
)
)
第三个参数$authns
可以控制是否只返回从授权DNS服务器中检索的NS记录。例如:
$records = dns_get_record("example.com", DNS_NS, true);
print_r($records);
该代码将只返回从授权DNS服务器中检索的NS记录,例如:
Array
(
[0] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns1.p03.dynect.net
)
[1] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns2.p03.dynect.net
)
[2] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns3.p03.dynect.net
)
[3] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => NS
[target] => ns4.p03.dynect.net
)
)
第四个参数$addtl
可控制是否返回在查询过程中发现的其他信息。例如:
$records = dns_get_record("example.com", DNS_A, false, true);
print_r($records);
该代码将返回以下数组,包含指向其他IP地址的在整个查询过程中找到的附加信息:
Array
(
[0] => Array
(
[host] => example.com
[class] => IN
[ttl] => 3599
[type] => MX
[pri] => 10
[target] => mx01.1and1.com
)
[1] => Array
(
[host] => example.com
[class] => IN
[ttl] => 21599
[type] => A
[ip] => 172.217.26.110
)
[2] => Array
(
[host] => mx01.1and1.com
[class] => IN
[ttl] => 10798
[type] => A
[ip] => 217.160.81.30
)
)
dns_get_record()
函数的使用取决于PHP环境的系统配置,如果没有正确安装或配置DNS解析器,则过程可能失败。dns_get_record()
函数不再支持ipv6记录类型的查询,因此建议使用其他函数或第三方库来获取ipv6记录。Net_DNS2
或dns_get_record()
作为替代方案。