Open In App

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:  

Return Value:  



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

Example 1:  




<?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
$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
$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
 


Article Tags :