php 处理dns_get_record的更好方法

68de4m5k  于 2023-02-18  发布在  PHP
关注(0)|答案(3)|浏览(136)

dns_get_record的行为

查询A记录:

变量转储(DNS获取记录($主机,DNS_A));

array(1) {
  [0]=>
  array(5) {
    ["host"]=>
    string(14) "host.name.tdl"
    ["class"]=>
    string(2) "IN"
    ["ttl"]=>
    int(0)
    ["type"]=>
    string(1) "A"
    ["ip"]=>
    string(12) "192.168.17.2"
  }
}

查询AAAA记录:

变量转储(DNS获取记录($主机,DNS AAAA));

PHP Warning:  dns_get_record(): DNS Query failed in file.php on line 4
bool(false)

我觉得这有点奇怪,没有记录返回为假,而不是作为空数组,但我想这是一种方法。

合并:

变量转储(DNS获取记录($主机,DNS A + DNS AAAA));

PHP Warning:  dns_get_record(): DNS Query failed in file.php on line 4
bool(false)

我的问题是:有没有更好的方法来查询A和AAAA记录,而不用执行两个dns_get_record调用?
$host的示例值

  • shaunwagner.com
  • digitec.ch

但基本上是所有没有IPv6的主机。

9jyewag0

9jyewag01#

我自己测试过之后,

var_dump(dns_get_record($host, DNS_A + DNS_AAAA));

工作刚刚好,这个方法也提到:
http://php.net/manual/en/function.dns-get-record.php

yrwegjxp

yrwegjxp2#

以下是使用**bluelibrary/dns**的替代方法

PHP代码

use BlueLibraries\Dns\Facade\DNS;
use BlueLibraries\Dns\Handlers\DnsHandlerTypes;
use BlueLibraries\Dns\Records\RecordTypes;

$records1 = dns_get_record('test.com', DNS_A + DNS_AAAA);

$records2 = DNS::getRecords(
    'test.com',
    [RecordTypes::A, RecordTypes::AAAA],
    DnsHandlerTypes::DIG
);

print_r($records1);
print_r($records2);

结果:

Array
(
    [0] => Array
        (
            [host] => test.com
            [class] => IN
            [ttl] => 0
            [type] => A
            [ip] => 67.225.146.248
        )

    [1] => Array
        (
            [host] => test.com
            [class] => IN
            [ttl] => 0
            [type] => AAAA
            [ipv6] => ::ffff:43e1:92f8
        )

)
Array
(
    [0] => BlueLibraries\Dns\Records\Types\A Object
        (
            [data:protected] => Array
                (
                    [host] => test.com
                    [ttl] => 0
                    [class] => IN
                    [type] => A
                    [ip] => 67.225.146.248
                )

        )

    [1] => BlueLibraries\Dns\Records\Types\AAAA Object
        (
            [data:protected] => Array
                (
                    [host] => test.com
                    [ttl] => 0
                    [class] => IN
                    [type] => AAAA
                    [ipv6] => ::ffff:67.225.146.248
                )

        )

)
mmvthczy

mmvthczy3#

var_dump(dns_get_record($host, DNS_ANY));
$tmp=array();
foreach(dns_get_record($host, DNS_ANY) as $dns){
    $tmp[$dns['type']]=$dns;
}
var_dump($tmp);

也许吧

相关问题