• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

PHP Domain availability check

Hi all,

I'm tinkering with a script to let people see if a domain is available to register or not.

The best way I've found so far is to use a whois script and then see if the response includes the words "Not found". I've had success with the one from here (near bottom):


Except I have the problem that it matches too generally, if I search for "microsoft.com" I get lots of jokes like:


So, I'm guessing if someone searched for example.com and someone else was using example.com.another-domain.com it's going to return that and my script, not finding a "Not found" message will claim the domain is taken even if example.com is in fact free.

Any suggestions? Is there a way I can stop matching these sub domains or does someone have a suggestion for a better script altogether? I mean, I don't need whois info, only a Yes or No when it comes to is a domain free or not.
  • 2
2 Solutions
Go to http://www.enom.com/resellers/default.asp?step=1#step1 and sign up for a free resellers account.  You can then use your login/pass to check domain name availability using this script I made:


// domain check via CURL:

$sld = $_POST['sld'];
$tld = $_POST['tld'];
$url = "http://reseller.enom.com/interface.asp?uid=YOURUIDHERE&pw=YOURPASSHERE&command=check&sld=$sld&tld=$tld&responsetype=text";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// grab URL and pass it to the browser
$serveroutput = curl_exec($ch);
$serveroutput = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $serveroutput);

// close curl resource, and free up system resources
echo "$extpage\n\n\n";

// watch the magic happen
$myarray = array();
$ex = explode("\n",$serveroutput);
foreach ($ex as $value) {
      if ($pos = strpos($value,"=")) {
            $mykey = substr($value, 0, $pos);
            $myvalue = substr($value, $pos+1);
            $myarray = $myarray + array($mykey => $myvalue);

echo "<pre>";
print_r ($myarray);


It will return something like:
    [RRPCode] => 211
    [RRPText] => Domain name not available
And you can just check if ($RRPCode==211), or if ($RRPText == "Domain name not available").

This script requires the CURL package to be installed and compiled with PHP.

You can also use sockets, but I recommend the CURL method.
KKPetersAuthor Commented:
That's great thanks, but if possible I'd prefer not to rely on someone elses service. Just incase they pull the plug or change the way it works, etc.

I'll use it there's no other alternative, but it just seem like I can't be the only person to have wanted to try this!
Well, you're GOING to rely on someone else's service no matter what. No two ways around that. Right now, you are using the services of one or more whois servers, which, as you note, could change the way it works or pull the plug (although it would kinda create havok by shutting down the whois servers, hehe).

Even when your script is working properly, even besides the false-positive problem you mentioned above, the data in the whois listings aren't guaranteed. It could show an expired (and available) domain as having a whois record, or vice versa. At least with the enom method, you will be getting accurate real-time domain name availability information.  Although I agree there exists the possibility that they change their policy such that you can't access their API with the free reseller account.

All in all, I'm willing to bet either method will work well enough for you.  The whois lookup method will work 99.9% of the time, so unless the reason for your script is to send them off to a domain registration site or something, you should be ok with it.  If indeed you know they are using your form for the purpose of finding domain names to register (and your affiliate registrar doesn't provide you with any API) then I say ride it out with enom for as long as it lasts, and use the whois method as a backup.

You might as well put both methods into the same form. If the enom lookup fails for whatever reason, it reverts to checking via the whois method. Can't lose that way :)
If you're willing to still use whois, you can pipe the results it gives through grep (a regular expression parser) to filter out joke domains. For example, to see if microsoft.com is available (it's not of course but I'm just giving an example), use:

whois microsoft.com | grep -i microsoft\.com$

That's a pipe symbol in the middle, a shift-\. The -i switch on grep tells it to ignore case. The backslash in microsoft.com after grep is there to ensure it's looking for a period, otherwise it could match any character. The dollar sign ensures that it only matches lines where microsoft.com appears at the very end of the line. If this returns the name of the domain, it's taken. If it returns nothing, it isn't. Be sure to do the escaping of the period, otherwise it might not work. This can be done with the preg_quote function in PHP. So in PHP, the popen function would look like:

popen("whois $domain | grep -i " . preg_quote($domain) . '$', "r");
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now