The PHP Extension and Application Repository (PEAR) has a handy Net/*
hierarchy
that includes a number of useful classes for interacting with network
functions. If you need to obtain DNS information, the Net_DNS class is for you.
It’s a free download
from PEAR
, and it’s actually a port of a Perl module called Net::DNS. The Net_DNS
package is actively maintained by Eric Kilfoil.

You should be aware of one important thing that distinguishes
Net_DNS from Net_Ping and
Net_Traceroute.
The Net_Ping and Net_Traceroute classes depend on system libraries to perform
their functions. Net_DNS, however, bypasses the system libraries and uses a
socket connection to communicate directly with DNS servers and retrieve the
requested records.

More detailed data

The information retrieved by the Net_DNS class is also
significantly more detailed than that generated by either Net_Ping or
Net_Traceroute. The class creates an object tree containing header, question,
answer, and authority records, each of which can be parsed and manipulated
individually.

The Net_DNS class uses the system’s resolv.conf file to
obtain the name server to query. If this file does not exist on your system,
you can create a file named resolv.conf in the same directory as your PHP
script and populate it with the following line (remember to adjust the IP
address to your local settings):

nameserver 200.200.200.30

To demonstrate basic usage of Net_DNS, consider the
following script:

<?php

// include class file
include(“Net/DNS.php”);

// create object
$ndr = new Net_DNS_Resolver();

// uncomment this for debug output
// $ndr->debug = 1;

// query for IP address
$answer = $ndr->search(“cnet.com”, “A”);

// print output
print_r($answer);
?>

The key here is the search() method, which accepts two
arguments: the domain name and the type of record to return (“A” for
IP address, “MX” for mail exchanger, and so on).

The
result of the query is created as a Net_DNS_Packet object, which internally
contains subobjects for the query sent, the response and RRs received, and the
authoritative name servers for those RRs. This is clearly illustrated in Listing A, which contains the output of
the script above.

Performing specific types of DNS queries

Each Net_DNS response object is made up of numerous subobjects.
The one you’ll usually be most interested in is the Net_DNS_RR object, which
contains the resource record returned by the server. Depending on the type of
query, one or more of these objects might be returned. You’ll need to manually
iterate over them and display their contents using the display() method. For
example, to obtain the MX record only, revise the script above as follows:

<?php

// include class file
include(“Net/DNS.php”);

// create object
$ndr = new Net_DNS_Resolver();

// query for IP address
$answer = $ndr->search(“cnet.com”, “MX”);

// print output
$answer->answer[0]->display();
echo “<br>”;
$answer->answer[1]->display();
?>

This script should output something like this:

cnet.com.     183    IN    MX     200    abv-sfo1-mail2.cnet.com.cnet.com.
cnet.com      183    IN    MX     100     c10-mail.cnet.com.

More PHP network functions

There are other useful classes in the Net/* hierarchy besides
Net_DNS, including support for traceroute
and ping.