PHP | dns_get_record() function

The dns_get_record() function is an inbuilt function in PHP which returns DNS resource records for the specified internet hostname.

Syntax:

dns_get_record($host, $type, $authoritative, $additional, $raw);

Parameter: This function accepts five parameters as mentioned above and described below:



  • $host: It is a required parameter. It specifies hostname whose DNS resource records to be found.
  • $type: It is an optional parameter. It specifies the type of DNS record to be searched. Possible values are
    • DNS_A
    • DNS_CNAME
    • DNS_HINFO
    • DNS_CAA
    • DNS_MX
    • DNS_NS
    • DNS_PTR
    • DNS_SOA
    • DNS_TXT
    • DNS_AAAA
    • DNS_SRV
    • DNS_NAPTR
    • DNS_A6
    • DNS_ALL
    • DNS_ANY (default)
  • $authoritative: It is an optional parameter. It is Passed by reference and, it will be populated with Resource Records for the Authoritative Name Servers if set.
  • $additional: It is an optional parameter. It is Passed by reference, it will be populated with any Additional Records if set.
  • $raw: It is an optional parameter. A Boolean parameter. If set to TRUE, Instead of looping type-by-type it queries only the requested type before getting the info stuff. FALSE is the default value.

Return Value:

  • It returns an array of associative arrays, FALSE on failure. Each array contains the following keys (atleast):
    • host: The hostname
    • class: This function only returns Internet class records so always return IN
    • type: Type of the record
    • ttl: “Time To Live” remaining for this record ( calculated as original ttl minus the
      length of time passed since the server was queried)

Note: This function is available for PHP 5.0.0 and newer version.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
print_r(dns_get_record("geeksforgeeks.org", DNS_MX));
?>

chevron_right


Output:

Array ( [0] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 1 [target] => aspmx.l.google.com ) [1] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => alt3.aspmx.l.google.com ) [2] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 10 [target] => alt4.aspmx.l.google.com ) [3] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt1.aspmx.l.google.com ) [4] => Array ( [host] => geeksforgeeks.org [class] => IN [ttl] => 299 [type] => MX [pri] => 5 [target] => alt2.aspmx.l.google.com ) )

Example 2: Systemetic Output

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$res=dns_get_record("geeksforgeeks.org", DNS_MX);
foreach($res as $ar){
  
    foreach($ar as $key=>$val){
            echo $key.":".$val."</br>";
    }
    echo "</br>";
}
  
?>

chevron_right


Output:

Example 3: all possible value of $type

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$domain="geeksforgeeks.org";
single_type_dns_get_record($domain, DNS_A);
single_type_dns_get_record($domain, DNS_CNAME);
single_type_dns_get_record($domain, DNS_HINFO);
single_type_dns_get_record($domain, DNS_CAA);
single_type_dns_get_record($domain, DNS_MX);
single_type_dns_get_record($domain, DNS_NS);
single_type_dns_get_record($domain, DNS_PTR);
single_type_dns_get_record($domain, DNS_SOA);
single_type_dns_get_record($domain, DNS_TXT);
single_type_dns_get_record($domain, DNS_AAAA);
single_type_dns_get_record($domain, DNS_SRV);
single_type_dns_get_record($domain, DNS_NAPTR);
single_type_dns_get_record($domain, DNS_A6);
single_type_dns_get_record($domain, DNS_ALL);
single_type_dns_get_record($domain, DNS_ANY);
  
function single_type_dns_get_record($domain, $type){
    echo "-------------<br>".$type."<br>-------------<br>";
    $res=dns_get_record($domain, $type);
    foreach($res as $ar){
        foreach($ar as $key=>$val){
                echo $key.":".$val."</br>";
        }
        echo "</br>";
    }
}
  
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.dns-get-record.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.