PHP怎么使用dns_get_record()函数?

2021年3月31日13:38:05 发表评论 924 次浏览

dns_get_record()函数是PHP中的内置函数, 它返回指定Internet主机名的DNS资源记录。

语法如下:

dns_get_record($host, $type, $authoritative, $additional, $raw);

参数:此函数接受上述和以下所述的五个参数:

  • $主机:必填参数。它指定要查找其DNS资源记录的主机名。
  • $类型:这是一个可选参数。它指定要搜索的DNS记录的类型。可能的值为
    • 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(默认)
  • $authoritative:它是一个可选参数。它通过引用传递, 并且将设置权威名称服务器的资源记录。
  • $ addalal:它是一个可选参数。它通过引用传递, 如果设置了任何附加记录, 它将被填充。
  • $ raw:它是一个可选参数。布尔参数。如果设置为TRUE, 则在获取信息之前, 不查询每个类型, 而是仅查询所请求的类型。 FALSE是默认值。

返回值:

  • 它返回一个关联数组的数组, 失败时返回FALSE。每个数组包含以下键(至少):
    • host:主机名
    • class:此函数仅返回Internet类记录, 因此始终返回IN
    • type:记录类型
    • ttl:该记录剩余的"生存时间"(以原始ttl减去
      自查询服务器以来经过的时间长度)

注意:此功能可用于PHP 5.0.0和更高版本。

范例1:

<?php
print_r(dns_get_record( "lsbin.com" , DNS_MX));
?>

输出如下(仅仅是示例):

Array ( [0] => Array ( [host] => lsbin.com [class] => IN [ttl] => 299 [type] => MX [pri] => 1 [target] => aspmx.l.google.com ) [1] => Array ( [host] => lsbin.com [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => alt3.aspmx.l.google.com ) [2] => Array ( [host] => lsbin.com [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => alt4.aspmx.l.google.com ) [3] => Array ( [host] => lsbin.com [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt1.aspmx.l.google.com ) [4] => Array ( [host] => lsbin.com [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt2.aspmx.l.google.com ) )

范例2:系统输出

<?php
$res =dns_get_record( "lsbin.com" , DNS_MX);
foreach ( $res as $ar ){
  
     foreach ( $ar as $key => $val ){
             echo $key . ":" . $val . "</br>" ;
     }
     echo "</br>" ;
}
  
?>

输出如下:

PHP怎么使用dns_get_record()函数?1

范例3:的所有可能值$类型

<?php
$domain = "lsbin.com" ;
single_type_dns_get_record( $domain , DNS_A);
single_type_dns_get_record( $domain , DNS_CNAME);
single_type_dns_get_record( $domain , DNS_HINFO);
single_type_dns_get_record( $domain , DNS_CAA);
single_type_dns_get_record( $domain , DNS_MX);
single_type_dns_get_record( $domain , DNS_NS);
single_type_dns_get_record( $domain , DNS_PTR);
single_type_dns_get_record( $domain , DNS_SOA);
single_type_dns_get_record( $domain , DNS_TXT);
single_type_dns_get_record( $domain , DNS_AAAA);
single_type_dns_get_record( $domain , DNS_SRV);
single_type_dns_get_record( $domain , DNS_NAPTR);
single_type_dns_get_record( $domain , DNS_A6);
single_type_dns_get_record( $domain , DNS_ALL);
single_type_dns_get_record( $domain , DNS_ANY);
  
function single_type_dns_get_record( $domain , $type ){
     echo "-------------<br>" . $type . "<br>-------------<br>" ;
     $res =dns_get_record( $domain , $type );
     foreach ( $res as $ar ){
         foreach ( $ar as $key => $val ){
                 echo $key . ":" . $val . "</br>" ;
         }
         echo "</br>" ;
     }
}
  
?>

输出如下:

PHP怎么使用dns_get_record()函数?2

参考: https://www.php.net/manual/en/function.dns-get-record.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: