2017-03-20 35 views
2

我打印使用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

爲什麼'aaaa'小寫,大寫? :) –

+0

@AlexandrKapustin:P我的錯誤..我試圖顯示我期望的輸出並使小寫... –

回答

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); 
相關問題