Hello Folks, I have adapted 'Mr Whois' to build an API to check if a domain is available for registration. This is the code I have so far:
<?php
define('COM_SERVER', "rs.internic.net");
define('COM_NOMATCH', "No match");
define('NET_SERVER', "rs.internic.net");
define('NET_NOMATCH', "No match");
define('ORG_SERVER', 'whois.publicinterestregistry.net');
define('ORG_NOMATCH', 'NOT FOUND');
$domain = $_GET['domain'];
$tld = $_GET['tld'];
function dispun() {
echo "0";
}
function dispav() {
echo "1";
}
if(ereg("^-|-$",$domain)) {
echo "bad domain - hypen";
exit;
}
if(!ereg("([a-z]|[A-Z]|[0-9]|-){".strlen($domain)."}",$domain)) {
echo "bad domain";
exit;
}
$domname = $domain . "." . $tld;
if ($tld == "com") {
$ns = fsockopen(COM_SERVER,43); fputs($ns,"$domname\r\n");
$result = '';
while(!feof($ns)) $result .= fgets($ns,128); fclose($ns);
if (eregi(COM_NOMATCH,$result)) { dispav();} else {dispun(); }
} elseif ($tld == "net") {
$ns = fsockopen(NET_SERVER,43); fputs($ns,"$domname\r\n");
$result = '';
while(!feof($ns)) $result .= fgets($ns,128); fclose($ns);
if (eregi(NET_NOMATCH,$result)) { dispav(); } else { dispun(); }
} elseif ($tld == "org") {
$ns = fsockopen(ORG_SERVER,43); fputs($ns,"$domname\r\n");
$result = '';
while(!feof($ns)) $result .= fgets($ns,128); fclose($ns);
if (eregi(ORG_NOMATCH,$result)) { dispav(); } else { dispun(); }
}
?>
You just go to: test.php?domain=google&tld=com
Now. My problem: It seems the code can't connect to the NIC servers. I have looked through time and time again and cannot see why it is not working. I am really stuck! It would be helpful to know if it worked on another PHP installation incase my host doesn't have sockets working properly.
I am so thankful in advance.
- James