Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Network Functions : dns_get_record

dns_get_record

Fetch DNS Resource Records associated with a hostname (PHP 5)
array dns_get_record ( string hostname [, int type [, array &authns, array &addtl]] )

Example 1591. Using dns_get_record()

<?php
$result
= dns_get_record("php.net");
print_r($result);
?>

The above example will output something similar to:

Array
(
   [0] => Array
       (
           [host] => php.net
           [type] => MX
           [pri] => 5
           [target] => pair2.php.net
           [class] => IN
           [ttl] => 6765
       )

   [1] => Array
       (
           [host] => php.net
           [type] => A
           [ip] => 64.246.30.37
           [class] => IN
           [ttl] => 8125
       )

)

Example 1592. Using dns_get_record() and DNS_ANY

Since it's very common to want the IP address of a mail server once the MX record has been resolved, dns_get_record() also returns an array in addtl which contains associate records. authns is returned as well containing a list of authoritative name servers.

<?php
/* Request "ANY" record for php.net,
  and create $authns and $addtl arrays
  containing list of name servers and
  any additional records which go with
  them */
$result = dns_get_record("php.net", DNS_ANY, $authns, $addtl);
echo
"Result = ";
print_r($result);
echo
"Auth NS = ";
print_r($authns);
echo
"Additional = ";
print_r($addtl);
?>

The above example will output something similar to:

Result = Array
(
   [0] => Array
       (
           [host] => php.net
           [type] => MX
           [pri] => 5
           [target] => pair2.php.net
           [class] => IN
           [ttl] => 6765
       )

   [1] => Array
       (
           [host] => php.net
           [type] => A
           [ip] => 64.246.30.37
           [class] => IN
           [ttl] => 8125
       )

)
Auth NS = Array
(
   [0] => Array
       (
           [host] => php.net
           [type] => NS
           [target] => remote1.easydns.com
           [class] => IN
           [ttl] => 10722
       )

   [1] => Array
       (
           [host] => php.net
           [type] => NS
           [target] => remote2.easydns.com
           [class] => IN
           [ttl] => 10722
       )

   [2] => Array
       (
           [host] => php.net
           [type] => NS
           [target] => ns1.easydns.com
           [class] => IN
           [ttl] => 10722
       )

   [3] => Array
       (
           [host] => php.net
           [type] => NS
           [target] => ns2.easydns.com
           [class] => IN
           [ttl] => 10722
       )

)
Additional = Array
(
   [0] => Array
       (
           [host] => pair2.php.net
           [type] => A
           [ip] => 216.92.131.5
           [class] => IN
           [ttl] => 6766
       )

   [1] => Array
       (
           [host] => remote1.easydns.com
           [type] => A
           [ip] => 64.39.29.212
           [class] => IN
           [ttl] => 100384
       )

   [2] => Array
       (
           [host] => remote2.easydns.com
           [type] => A
           [ip] => 212.100.224.80
           [class] => IN
           [ttl] => 81241
       )

   [3] => Array
       (
           [host] => ns1.easydns.com
           [type] => A
           [ip] => 216.220.40.243
           [class] => IN
           [ttl] => 81241
       )

   [4] => Array
       (
           [host] => ns2.easydns.com
           [type] => A
           [ip] => 216.220.40.244
           [class] => IN
           [ttl] => 81241
       )

)

Code Examples / Notes » dns_get_record

marcus

The docs say "nor does it (currently) work on *BSD systems". Unfortunately this includes MacOS X. It's just not there in my build of 5.1.2 running on 10.4.4.

axelm-php

I've done a backport of the function to PHP4.
It is available as extension at:
http://nona.net/software/dns_get_record/
comments & feedback appreciated.
--
Alexander Mayrhofer
axelm-php@nona.net


ed

I recently needed to do some name server and IP discovery on the fly.
Server and domain monitoring system.  If you've got a system with dig
the below seems to work fine for discovering name server and a
addresses.  Shouldn't be hard for you to modify it for the other records if
needed.
$_REQUEST[url] = "http://www.example.com/";
$url_parts = parse_url($_REQUEST[url]);
$dns = Dig($url_parts[host]);
print_r($dns[name_servers]);
print_r($dns[ips]);
function Dig ($domain) {
$dig = `dig $domain`;
preg_match_all("/in\s+ns\s+(.+?)\s+/is"
,$dig,$name_servers,PREG_PATTERN_ORDER);
preg_match_all("/$domain.\s+[0-9]+\s+in\s+a\s+([0-9.]+)\s+/is"
,$dig,$ips,PREG_PATTERN_ORDER);
$dns[name_servers] = $name_servers[1];
$dns[ips] = $ips[1];
return($dns);
}


Change Language


Follow Navioo On Twitter
checkdnsrr
closelog
debugger_off
debugger_on
define_syslog_variables
dns_check_record
dns_get_mx
dns_get_record
fsockopen
gethostbyaddr
gethostbyname
gethostbynamel
getmxrr
getprotobyname
getprotobynumber
getservbyname
getservbyport
header
headers_list
headers_sent
inet_ntop
inet_pton
ip2long
long2ip
openlog
pfsockopen
setcookie
setrawcookie
socket_get_status
socket_set_blocking
socket_set_timeout
syslog
eXTReMe Tracker