Open In App

PHP | dns_get_record() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:  

PHP




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


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: Systematic Output  

PHP




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


Output: 

Example 3: all possible value of $type 

PHP




<?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>";
    }
}
 
?>


Output: 

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



Last Updated : 07 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads