Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Using Sockets in PHP
MISCELLANEOUS

Using Sockets in PHP
By: Andrew Walsh
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2004-06-24

    Table of Contents:
  • Using Sockets in PHP
  • Making the Connection
  • Sending The Request
  • Searching for a Page
  • Whois Example
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Using Sockets in PHP - Searching for a Page


    (Page 4 of 6 )

    Right in this section I will show you an example that uses fsockopen() to connect to a server. The example I will show you is how to connect to multiple webservers and check if a certain page is on that server.

    <?php
    $servers 
    = array(
                   
    "www.example.com" =&gt"/index.html",
                   
    "www.example2.com" =&gt"/index.php"
    );
    /*
    loop through the servers array and then connect to
    the host and return an error if fsockopen couldn't
    connect to the server.
    */
    foreach($servers as $host=&gt$page){
    $fp fsockopen($host,80,$errno,$errdesc,10);
    echo 
    "Trying $host&lt;br&gt;&#92;n";
    if(!
    $fp){
    echo(
    "couldnt connect to $host");
    echo 
    "&lt;br&gt;&lt;hr&gt;&lt;br&gt;&#92;n";
    continue;
    }
    /*
    Print trying to get the page then define the
    request and send it to the server
    */
    echo "trying to get $page&lt;br&gt;&#92;n";
    $request "HEAD $page HTTP/1.0&#92;r&#92;n&#92;r&#92;n";
    fputs($fp$request);
    /*
    print the results to the browser
    */
    echo fgets($fp1024);
    echo 
    "&lt;br&gt;&lt;br&gt;&lt;br&gt;&#92;n";
    /*
    Once again close the connection with fclose()
    */
    fclose($fp);
    /*
    Close the foreach loop
    */
    }
    ?>

    That piece of code will simply display something like this:

    Trying: www.example.com
    Trying to get: /index.html
    HTTP/1.1 200 OK

    That will be displayed only if the page was found. 404 Not found will replace 200 OK if the page wasn't found. You may recognize the 404 error as the exact same one as seen in your browser if you go to a page that doesn't exist.

    In the next section I will show you how to get whois results for a domain name using fsockopen().

    More Miscellaneous Articles
    More By Andrew Walsh

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap