If I have a domain like www.example.com and I want to check if it is available using DNS records (not whois)...
Is it possible to do this using PHP?
If I have a domain like www.example.com and I want to check if it is available using DNS records (not whois)...
Is it possible to do this using PHP?
You can use checkdnsrr or gethostbyname:
Documentation:
http://www.php.net/gethostbyname
Example checkdnsrr:
<?php
if ( checkdnsrr('example.com.', 'ANY') ) {
echo "DNS Record found";
}
else {
echo "NO DNS Record found";
}
?>
Example gethostbyname:
<?php
$domain = 'example.com';
if ( gethostbyname($domain) != $domain ) {
echo "DNS Record found";
}
else {
echo "NO DNS Record found";
}
?>
It depends on what you mean by "available." If you mean available for registration, it is not possible to determine based on DNS information alone. The whois system must be used. An easy way to test is to take an unused domain name and set the nameservers to something invalid. DNS will not be available, but the domain is still unavailable for registration. I just tested the suggestions of checkdnsrr(), gethostbyname(), and dns_get_record(). All show that no DNS is returned for a domain that cannot be registered.
The following question offers some more details: Checking if a domain name is registered
All answers suggest using some PHP library. There's a function in vanilla PHP (version 5 to 7) that fetches DNS records for a specific domain.
Utilising the following PHP line,
sizeof(dns_get_record("example.com"))
you will get an INT corresponding to the number of DNS records. Therefore, consider something alike:
if (sizeof(dns_get_record("example.com")) > 0) echo "Domain has DNS records";
else echo "Domain does not have any DNS records";
I should go for this package Domain-Availability it's support a lot of Top Level Domains and is written in a Object Oriented way.
Here's a script to check domain availability for some domains using whois. In this example we check all number domains between 10 and 999 for a given top level domain (spoiler alert: they're mostly taken).
Here are predefined examples for .com .pe .ee .cc and .gg. It runs a whois search for the domain and then looks for the words "no match for domain" in the output.
$tld = "com" ;
$lookFor = array();
$lookFor['com'] = "No match for domain" ; // .com
$lookFor['pe'] = "No Object Found" ; // .pe
$lookFor['ee'] = "Domain not found" ; // ee
$lookFor['cc'] = "No match for" ; // cc
$lookFor['gg'] = "NOT FOUND" ;
$i = 10 ;
while($i < 10000)
{
$domain = "$i.$tld" ;
$dig = `whois $domain` ;
if(stripos($dig, $lookFor[$tld]) !== FALSE)
{
echo "$domain is available\n" ;
} else {
echo "$domain bad\n" ;
}
$i ++ ;
}
Some of the API that will do the job.
http://www.whoisxmlapi.com/domain-availability.php