≡ Menu

How to use the PHP package Net_DNS2? With Examples…

Net_DNS2 is a PHP pear package which allows you to run DNS queries from PHP code. This is very useful in several situations in which you need to extract DNS records of a domain name. Net_DNS2 is compatible with both IPv6 and DNSSEC. The authors claim that Net_DNS2 is significantly faster than its precursor Net_DNS. I could not see the difference but it is nonetheless useful to upgrade if you are using Net_DNS.

The main object in Net_DNS2 is the Net_DNS2_Resolver. This class allows you to do all kinds of DNS queries including ANY, A, MX, CNAME, NS and any other RR type in the DNS ecosystem. In this tutorial, I will show with examples, how to use Net_DNS2 for performing DNS queries.

The following shows a simple piece of code that uses Net_DNS2_Resolver to query for A records for google.com:

$resolver = new Net_DNS2_Resolver();
$resolver->timeout = 1;
$response = 0;
try
{
$response = $resolver->query("google.com", "A");
}
catch(Net_DNS2_Exception $e)
{}

I’ll explain the code in detail. The first line creates an object of type Net_DNS2_Resolver. The second line says that the timeout for the DNS query is 1 second. The sixth line does the actual DNS query. It passes in the domain name google.com and the record type A as arguments to the query method in the resolver object. The resolver object returns the response. More on the response object later. As in the above code, if you do not specify a name server, Net_DNS2 will use the open resolvers in the resolv.conf file on a Linux system and something similar on a Windows based system.

Now suppose you want to query a particular name server for the A records of google.com. It is very simple to do that. However, note that you will first need to convert the name server name into its IP address as Net_DNS2 takes in the IP address of the name server as an argument.

$nsr = array("nameservers" => array("8.8.8.8"));
$resolver = new Net_DNS2_Resolver($nsr);
$resolver->timeout = 1;
try
{
$response = $resolver->query($domain, $record);
}
catch(Net_DNS2_Exception $e)
{}

The first line in the above snippet of code creates an array of name servers to query. In this case, it is only one IP address 8.8.8.8 (which is the IP address of the name server of Google public DNS). In this case, Net_DNS2_Resolver will use 8.8.8.8 as the name server and query it. The response object is the same as before.

Now I will discuss the response object. The response object contains several parts – $response->answer (which is the answer to the DNS query), $response->additional (this is the additional section), $response->authority (this is the authority section) and so on. You can use the PHP command print_r($response) to see the whole object printed recursively.

To iterate through the response object, we need a PHP loop.

foreach ($response->answer as $rrr)
{
if($rrr->name == "google.com" && $rrr->type == 'A' && $rrr->address != "")
{
echo $rrr->address;
}
}

The above foreach statement loops through all A records of google.com returned by our previous code snippets. Each value in the array $response->answer contains several fields like name (which is the domain name), type (which is “A”, “MX” and so on) and $rrr->address (which is the IP address associated with the A record.

Now if you would query for an “MX” record, the type would be “MX” and the field that contains the MX record is called exchange.

Here are the various RR types and the corresponding field names (You can also do a print_r($response) to see everything inside the $response object).

A – address, MX – exchange, AAAA – address, NS – nsdname, CNAME – cname, TXT – text (this could be an array), SOA – serial/refresh/retry/expire/minimum/mname/rname.

Thats it! You are now ready to use Net_DNS2 to perform DNS lookups from your PHP code. If you have any questions, please don’t hesitate to leave comments. Hope this article was of use to you!

Rahul

{ 0 comments… add one }

Leave a Comment