How to check domain availability in PHP?
Following example shows you how to check the availability of a domain name using PHP.
Domain Name System (DNS) is a naming system for resources connected to the Internet or any other personal network. It contains details of registered domain names in the world up to date. It is a large database of domain names and the IP address mapped to that domain. A Domain Name Registrar is an organization or commercial entity that manage the reservation of Internet domain names. Domain Name Registar provides the registration service for end-users.
Before you register a domain name, you may need to check the availability of the domain name. This example is based on the www.whoisxmlapi.com Domain Availability API. First you have to create a free account with whoisxmlapi site. They will provide an account with free 500 lookups for checking the availability of domain names. Also they provide hosted whois web service plans. By using this free account you can send up to 500 lookup quires. Also they provide a way to check your remaining lookups and e-mail alert at your threshold limit. They provide an easy way to check the domain availability and whois lookup for a given domain. You can configure this to your web application using following example code.
You can get the output as JSON or XML. To do that you have to set outputFormat to JSON or XML. Note that you will be need to connect to the internet to check this example.
<?php
/*
©-Copyright by www.latestcode.net
*/
class CheckDomainNameAvailability{
const WHOIS_SERVICE_URL = "http://www.whoisxmlapi.com/whoisserver/WhoisService?";
const ACCOUNT_SERVICE_URL = "http://www.whoisxmlapi.com/accountServices.php?";
private $userName;
private $password;
private $domainName;
private $outputFormat;
private $isDomainAvailable=FALSE;
private $accountBalance;
public function __construct() {
$this->userName='username';
$this->password='password';
$this->outputFormat = 'JSON';//You can set XML also
}
/**
* Check domain name availability
* @param string $domainName
* @return boolean
*/
public function getDomainNameAvailability($domain){
$this->domainName = str_replace('www.', '', strtolower($domain));
$domain_details = @file_get_contents(self::WHOIS_SERVICE_URL."cmd=GET_DN_AVAILABILITY&domainName=".$this->domainName."&username=".$this->userName."&password=".$this->password."&outputFormat=".$this->outputFormat);
$result = json_decode($domain_details);
$this->isDomainAvailable = ($result->DomainInfo->domainAvailability==='AVAILABLE')?TRUE:FALSE;
return $this->isDomainAvailable;
}
/**
* Check available lookups
* @return bool
*/
public function getAvailableLookups(){
$accountDetails = @file_get_contents(self::ACCOUNT_SERVICE_URL."servicetype=accountbalance&username=".$this->userName."&password=".$this->password);
$xml='<xml>'.$accountDetails.'</xml>';
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
if($xmlDoc->hasChildNodes()){
$balance = $xmlDoc->getElementsByTagName('balance');
if($balance->length>0){
$this->accountBalance = $balance->item(0)->nodeValue;
}else{
$this->accountBalance = 0;
}
unset($xmlDoc);
}else{
exit('An unexpected error has occured');
}
return $this->accountBalance;
}
}
/*
©-Copyright by www.latestcode.net
*/
class CheckDomainNameAvailability{
const WHOIS_SERVICE_URL = "http://www.whoisxmlapi.com/whoisserver/WhoisService?";
const ACCOUNT_SERVICE_URL = "http://www.whoisxmlapi.com/accountServices.php?";
private $userName;
private $password;
private $domainName;
private $outputFormat;
private $isDomainAvailable=FALSE;
private $accountBalance;
public function __construct() {
$this->userName='username';
$this->password='password';
$this->outputFormat = 'JSON';//You can set XML also
}
/**
* Check domain name availability
* @param string $domainName
* @return boolean
*/
public function getDomainNameAvailability($domain){
$this->domainName = str_replace('www.', '', strtolower($domain));
$domain_details = @file_get_contents(self::WHOIS_SERVICE_URL."cmd=GET_DN_AVAILABILITY&domainName=".$this->domainName."&username=".$this->userName."&password=".$this->password."&outputFormat=".$this->outputFormat);
$result = json_decode($domain_details);
$this->isDomainAvailable = ($result->DomainInfo->domainAvailability==='AVAILABLE')?TRUE:FALSE;
return $this->isDomainAvailable;
}
/**
* Check available lookups
* @return bool
*/
public function getAvailableLookups(){
$accountDetails = @file_get_contents(self::ACCOUNT_SERVICE_URL."servicetype=accountbalance&username=".$this->userName."&password=".$this->password);
$xml='<xml>'.$accountDetails.'</xml>';
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
if($xmlDoc->hasChildNodes()){
$balance = $xmlDoc->getElementsByTagName('balance');
if($balance->length>0){
$this->accountBalance = $balance->item(0)->nodeValue;
}else{
$this->accountBalance = 0;
}
unset($xmlDoc);
}else{
exit('An unexpected error has occured');
}
return $this->accountBalance;
}
}
Usage:
$dnc = new CheckDomainNameAvailability();
//To check domain name availability - you can enter domain name witho or without 'www'.
$domainaName = 'www.test.com';
$available = $dnc->getDomainNameAvailability($domainaName);
echo $domainaName." is ".(($available==FALSE)?" not ":" ")."available";
//To check account balance
$balance = $dnc->getAvailableLookups();
if($balance>0){
echo "You have ".$balance." remaining lookups";
}else{
echo "Your account balance is zero";
}
$dnc = new CheckDomainNameAvailability();
//To check domain name availability - you can enter domain name witho or without 'www'.
$domainaName = 'www.test.com';
$available = $dnc->getDomainNameAvailability($domainaName);
echo $domainaName." is ".(($available==FALSE)?" not ":" ")."available";
//To check account balance
$balance = $dnc->getAvailableLookups();
if($balance>0){
echo "You have ".$balance." remaining lookups";
}else{
echo "Your account balance is zero";
}
7 comments:
Above excellent guide you have shared about this concept and it is really great for me to found this blog.Register a Domain
Hi,
thanks for sharing.
You explained the process to check domain availability in PHP in a very informative way
i have copied yr code but every time it give output like "Domain name is not available". plz help me..
email- smartnisarg2310@gmail.com
Hey I have found and api for domain check -> http://freedomainapi.com
Id rather do this my own way, so I created a PHP Class for this.
Grab the code: http://helgesverre.com/blog/php-domain-availability-checking-script/
No one can stop you writing a perfect blog or article if you are passionate to your job. Your aim is to satisfy your readers with a well written blog and your passion is always behind your every successful assignment. Impressive writing skills can be observed over here.
now present in your city
Post a Comment