You could try searching the snippets databases here, at px.sklar.com, and at zend.com to get answers to this sort of question. There are at least half a dozen implementations floating around.
Here is one that I wrote. It queries the .com/.net/.org whois servers and also will use ARIN to reverse-lookup names from IP numbers.
<?PHP
function getnumericaddress($domain)
{
echo "<b>Network number information from ARIN</b>";
echo "<PRE>";
$fp = fsockopen("whois.arin.net", 43, &$errno, &$errstr, 10);
if(!$fp)
{
echo "Could not open connection to $server on port 43.\n";
echo "$errstr ($errno)<br>\n";
}
else
{
fputs($fp,"$domain\r\n");
while(!feof($fp))
{
echo fgets($fp,128);
}
fclose($fp);
}
echo "</PRE>";
}
if ($domain)
{
// if no alpha chars, assume it is a numeric address
// and try to get results from whois.arin.net
if ( ereg( "([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})" , $domain, $regs))
{
getnumericaddress($domain);
}
else {
// first get the domain server from internic
$fp = fsockopen("rs.internic.net", 43, &$errno, &$errstr, 10);
if(!$fp)
{
echo "$errstr ($errno)<br>\n";
}
else
{
fputs($fp,"$domain\r\n");
while(!feof($fp))
{
$buf = fgets($fp,128);
if (ereg("Whois Server:", $buf))
{
$server = str_replace("Whois Server: ", "", $buf);
$server = trim($server);
}
}
fclose($fp);
}
if ($server)
{
echo "<B>$domain is registered at $server:</B><BR>";
echo "<PRE>";
$fp = fsockopen($server, 43, &$errno, &$errstr, 10);
if(!$fp)
{
echo "Could not open connection to $server on port 43.\n";
echo "$errstr ($errno)<br>\n";
}
else
{
fputs($fp,"$domain\r\n");
while(!feof($fp))
{
echo fgets($fp,128);
}
fclose($fp);
}
}
else {
echo("<b>$domain does not appear to be registered.</b><BR>");
}
echo ("</PRE><BR>");
}
}
?>
<FORM ACTION="<?PHP echo($PHP_SELF); ?>" METHOD="post">
This will find .com, .org, and .net domains and most IP (netblock) assignments<br>
domain: <INPUT TYPE="text" NAME="domain" SIZE="40" MAXLENGTH="100">
<INPUT TYPE=submit VALUE="Find out"><INPUT TYPE=reset VALUE="Reset">
</FORM>