我使用PHP打印网站的DNS记录。但我所期待的输出与我得到的不同。这是我得到的代码和输出:
<?php
$domain = "php.net";
$result = dns_get_record($domain,DNS_ANY);
echo '<pre>';
echo json_encode(array('domain'=>$domain,'data'=>$result), JSON_PRETTY_PRINT);
?>
输出:
{
"domain": "php.net",
"data": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"type": "AAAA",
"ipv6": "2a02:cb41::7"
},
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"type": "TXT",
"txt": "v=spf1 ip4:72.52.91.12 ip6:2a02:cb41::8 ip4:140.211.15.143 ?all",
"entries": [
"v=spf1 ip4:72.52.91.12 ip6:2a02:cb41::8 ip4:140.211.15.143 ?all"
]
},
{
"host": "php.net",
"class": "IN",
"ttl": 30,
"type": "MX",
"pri": 0,
"target": "php-smtp2.php.net"
},
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"type": "SOA",
"mname": "ns1.php.net",
"rname": "admin.easydns.com",
"serial": 1484930803,
"refresh": 16384,
"retry": 2048,
"expire": 1048576,
"minimum-ttl": 2560
},
{
"host": "php.net",
"class": "IN",
"ttl": 185,
"type": "A",
"ip": "72.52.91.14"
},
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"type": "NS",
"target": "dns3.easydns.org"
},
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"type": "NS",
"target": "dns2.easydns.net"
},
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"type": "NS",
"target": "dns1.easydns.com"
},
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"type": "NS",
"target": "dns4.easydns.info"
}
]
}
我期待输出如下:
{
"domain": "php.net",
"data": [
{
"AAAA": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"ipv6": "2a02:cb41::7"
}
]
},
{
"TXT": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"txt": "v=spf1 ip4:72.52.91.12 ip6:2a02:cb41::8 ip4:140.211.15.143 ?all",
"entries": [
"v=spf1 ip4:72.52.91.12 ip6:2a02:cb41::8 ip4:140.211.15.143 ?all"
]
}
]
},
{
"MX": [
{
"host": "php.net",
"class": "IN",
"ttl": 30,
"pri": 0,
"target": "php-smtp2.php.net"
}
]
},
{
"SOA": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"mname": "ns1.php.net",
"rname": "admin.easydns.com",
"serial": 1484930803,
"refresh": 16384,
"retry": 2048,
"expire": 1048576,
"minimum-ttl": 2560
}
]
},
{
"A": [
{
"host": "php.net",
"class": "IN",
"ttl": 185,
"ip": "72.52.91.14"
}
]
},
{
"NS": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"target": "dns3.easydns.org"
}
]
},
{
"NS": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"target": "dns3.easydns.org"
}
]
},
{
"NS": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"target": "dns3.easydns.org"
}
]
},
{
"NS": [
{
"host": "php.net",
"class": "IN",
"ttl": 300,
"target": "dns3.easydns.org"
}
]
}
]
}
即使我想知道为什么DNS_ANY
工作而不是DNS_ALL?另外我如何避免输出中的重复条目,因为可以看到与type: NS
关联的记录是重复输出。
请帮我解决这个问题的答案。
答案 0 :(得分:3)
<?php
$domain = "php.net";
$result = dns_get_record($domain,DNS_ANY);
$data = [];
foreach ($result as $item) {
$type = $item['type'];
unset($item['type']);
$data[] = [$type => [$item]];
}
echo '<pre>';
echo json_encode(array('domain'=>$domain,'data'=>$data), JSON_PRETTY_PRINT);