Net_DNS_Resolver::query() – Queries a nameserver and returns a response



Net_DNS_Packet Net_DNS_Resolver::query ( string $hostname , string $type = 'A' , string $class = 'IN' )


  • hostname - The name to lookup (eg.

  • type - The record type to query

  • class - The zone class to query

Constructs a DNS query packet and forwards the query to a nameserver configured in the system stub resolver (ie. /etc/resolv.conf). When a response is received from the DNS server, a fully populated Net_DNS_Packet object is returned that represents the response.

The resolver object contains many properties that control that behaviour of the resolver. Some of these settings are automatically read from the system resolver configuration if available. On Linux/UNIX based systems, this includes /etc/resolv.conf as well as various environment variables. This configuration is done at the time of object instantiation and can be overridden by setting the appropriate object properties.

This function will only return a Net_DNS_Packet if the ANSWER section contains resource records. Specifically, if the ANCOUNT variable in the DNS packet header is 0, query() will return 0 (note: 0, not FALSE). If you are expecting a packet without resource records in the ANSWER section, use Net_DNS_Resolver::rawQuery(). This is useful when doing manual recursion.

For a description of the returned RR data object, see Net_DNS_RR.

Resolver Configuration Object Properties:

  • array $nameservers

    An array of nameserver IP addresses that should be queried.

  • int $port

    The port on which nameservers should be queried. The default is 53.

  • array $domain

    The domain in which the resolver client host resides.

  • array $searchlist

    An array of strings containingg domains to apply to unqualified hosts passed to the resolver.

  • int $retry

    The number of seconds between retransmission of unaswered queries

  • int $retrans

    The number of times unanswered requests should be retried

  • int $recurse

    Sets the value of the RD (recursion desired) bit in the header. If the RD bit is set to 0, the server will not perform recursion on the request.

  • int $usevc

    Whether or not to use TCP (Virtual Circuits) instead of UDP If set to 0, UDP will be used unless TCP is required. TCP is required for questions or responses greater than 512 bytes.

  • int $debug

    If set to TRUE (non-zero), debugging code will be displayed as the resolver makes the request.

Environment Variables:


    Space separated list of nameserver IP addresses to query


    Space separated list of domain names to add to unqualified search requests.


    The name of the domain


    A space separated list of options formatted as:


    If the value is ommited, the value defaults to 1 (true). [optionname] corresponds to an object property.


Using Net_DNS_Resolver::query()

require_once 'Net/DNS.php';

$resolver = new Net_DNS_Resolver();
$response $resolver->query('');
if (
$response) {
  foreach (
$response->answer as $rr) {

Output:            129808  IN      A

The following example shows a DNS query for an MX record. Note that the IP address for the mail exchanger listed within the zone is returned with the response in the additional section. The second exchanger (that is not inside this zone) is not listed. To receive this address, you must perform another query specifically for the A record using the returned hostname.

Using Net_DNS_Resolver::query() to look up an MX record

require_once 'Net/DNS.php';

$resolver = new Net_DNS_Resolver();
$response $resolver->query('''MX');
if (
$response) {
  foreach (
$response->answer as $rr) {
  if (
count($response->additional)) {
    foreach (
$response->additional as $rr) {

Output:                86121   IN      MX      15                86121   IN      MX      5           86121   IN      A

The next example shows a more complex query with debugging information enabled. Note that the usevc option is set to TRUE. This forces the resolver to use TCP instead of UDP. This can be seen in the debug output on the send_tcp() line.

Using Net_DNS_Resolver::query() with specific nameservers and options

require_once 'Net/DNS.php';

$resolver = new Net_DNS_Resolver();
$resolver->debug 1// Turn on debugging output to show the query
$resolver->usevc 1// Force the use of TCP instead of UDP
$resolver->nameservers = array(              // Set the IP addresses
'',     // of the nameservers
''  // to query.
$response $resolver->query('');
if (! 
$response) {
"ANCOUNT is 0, therefore the query() 'failed'\n";
"See Net_DNS_Resolver::rawQuery() to receive this packet\n";


;; query(, A, IN)
;; send_tcp(
;; sending 29 bytes
;; received 517 bytes
;; id = 58298
;; qr = 1    opcode = QUERY    aa = 0    tc = 0    rd = 1
;; ra = 0    rcode  = NOERROR
;; qdcount = 1  ancount = 0  nscount = 13  arcount = 15

;; QUESTION SECTION (1 record)
;   IN      A

;; ANSWER SECTION (0 records)

;; AUTHORITY SECTION (13 records)
com.            172800  IN      NS      A.GTLD-SERVERS.NET.
com.            172800  IN      NS      G.GTLD-SERVERS.NET.
com.            172800  IN      NS      H.GTLD-SERVERS.NET.
com.            172800  IN      NS      C.GTLD-SERVERS.NET.
com.            172800  IN      NS      I.GTLD-SERVERS.NET.
com.            172800  IN      NS      B.GTLD-SERVERS.NET.
com.            172800  IN      NS      D.GTLD-SERVERS.NET.
com.            172800  IN      NS      L.GTLD-SERVERS.NET.
com.            172800  IN      NS      F.GTLD-SERVERS.NET.
com.            172800  IN      NS      J.GTLD-SERVERS.NET.
com.            172800  IN      NS      K.GTLD-SERVERS.NET.
com.            172800  IN      NS      E.GTLD-SERVERS.NET.
com.            172800  IN      NS      M.GTLD-SERVERS.NET.

;; ADDITIONAL SECTION (15 records)
A.GTLD-SERVERS.NET.     172800  IN      AAAA    2001:503:a83e::2:30
A.GTLD-SERVERS.NET.     172800  IN      A
G.GTLD-SERVERS.NET.     172800  IN      A
H.GTLD-SERVERS.NET.     172800  IN      A
C.GTLD-SERVERS.NET.     172800  IN      A
I.GTLD-SERVERS.NET.     172800  IN      A
B.GTLD-SERVERS.NET.     172800  IN      AAAA    2001:503:231d::2:30
B.GTLD-SERVERS.NET.     172800  IN      A
D.GTLD-SERVERS.NET.     172800  IN      A
L.GTLD-SERVERS.NET.     172800  IN      A
F.GTLD-SERVERS.NET.     172800  IN      A
J.GTLD-SERVERS.NET.     172800  IN      A
K.GTLD-SERVERS.NET.     172800  IN      A
E.GTLD-SERVERS.NET.     172800  IN      A
M.GTLD-SERVERS.NET.     172800  IN      A

ANCOUNT is 0, therefore the query() 'failed'
See Net_DNS_Resolver::rawQuery() to receive this packet


This function can not be called statically.

Provides a resolver implementation to handle DNS queries (Previous) Queries a nameserver and returns a response (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.