Reputation: 7273
I am print the DNS Record of a website using PHP. But the output I am epecting is something different than what I got. Here is the code and output I am getting:
<?php
$domain = "php.net";
$result = dns_get_record($domain,DNS_ANY);
echo '<pre>';
echo json_encode(array('domain'=>$domain,'data'=>$result), JSON_PRETTY_PRINT);
?>
The output:
{
"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"
}
]
}
I am expecting the output to be like this:
{
"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"
}
]
}
]
}
Even I would like to know why the DNS_ANY
work and not DNS_ALL? Also how I can avoid the duplicate entries in my output, as one can see the the record associated with the type: NS
is repeated output.
Kindly help me with this question's answer.
Upvotes: 2
Views: 64
Reputation: 2449
<?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);
Upvotes: 3