Hudzilla.org - the homepage of Paul Hudson
Contents > Networks > Sockets Wish List | Report Bug | About Me ]

15.1.3     Sockets aren't all about HTTP

This is NOT the latest copy of this book; click here for the latest version.

While receiving compressed content is not a particularly amazing thing to do with fsockopen(), it should at least demonstrate the fact that it gives you a lot more control over your sockets than simply using fopen(). For example, if we wanted to do something other than read HTTP, we just need to change the port number - here is a script that reads in Whois information about a site:

<?php
    $fp
= fsockopen ("whois.networksolutions.com", 43);

    if (
$fp) {
        
fwrite($fp, "microsoft.com\n");

        while (!
feof($fp)) {
            print
fread($fp,256);
        }

        
fclose ($fp);
    } else {
        print
"Fatal error\n";
    }
?>

It looks similar to the last one, as I am sure you can see. The difference is that this time we connect to the Whois database of Network Solutions, which contains registrar information for .com, .net, and .org domains, and we query it for information on microsoft.com. Port 43 is the "whois" port, used for these kind of queries, which means that the Network Solutions server is listening for clients who connect on port 43. As soon as we connect, we provide a domain name followed by a line return, which is part of the very basic Whois protocol - doing this signals to the Whois server that we want it to return Whois information on the server we specified.

Give that script a try, although it is only for trial purposes alone - as you will see in the output, Network Solutions request that you don't use their Whois database as part of "high-volume, automated, electronic processes". Please remember that the Internet relies on a lot of co-operation between people, so respect their wishes!





<< 15.1.2 Making a simple search engine   15.1.4 Sockets can be servers: socket_create_listen(), socket_accept(), socket_write(), socket_read(), and socket_close() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
ramavorray@yahoo.com - 06 Sep 2008

sorry, forgot to enter my email address before.

A PHP User - 06 Sep 2008

when I tried this using microsoft.com I just received this 'No match for "MICROSOFT.COM". ' along with copyright message of networksolutions.com, this only worked for networksolutions.com domain, and for no other website.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow