Using Sockets in PHP - Whois Example
(Page 5 of 6 )
So you have learned how to connect to servers, fetch results, loop through results and finally close connections. So I thought I would include a more practical example in this section and I chose to do a whois example which connects to certain whois servers and checks for a record for a certain domain.
<?php extract($_POST); function whois($domain,$ext){ $url=$domain.$ext; /* Use a switch() statement to determine which whois server is the best to use for the entered domain. */ switch($ext){ case ".co.uk": $whois = "whois.nic.uk"; break; case ".com": $whois = "whois.networksolutions.com"; break; case ".fr": $whois = "whois.nic.fr"; break; case ".biz": $whois = "whois.biz"; break; default: $whois = "whois.networksolutions.com"; } if (trim($url) <> "") { $url = trim($url); /* Open the connection to the above whois server */ $f = fsockopen($whois, 43, $errno, $errstr, 30); if (!$f) { echo "Connection To Server Failed ($errno)"; } else { /* Send the domain to the server and return the results */ fputs($f, "$url\r\n"); print "<pre>\r\n"; while (!feof($f)) { echo fread($f,128); } print "</pre>"; /* Use fclose to close the connection to the whois server */ fclose($f); } }else{ echo "Invalid domain entered"; } } ?> |
Notice I have written this code in a function so it is more practical and reusable than the other examples in this tutorial. They can be coded up into a function with relative ease.
Next: Conclusion >>
More Miscellaneous Articles
More By Andrew Walsh