Miscellaneous

  Home arrow Miscellaneous arrow Page 5 - 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 - 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) &lt;&gt"") { 
      
    $url trim($url);
    /*
    Open the connection to the above whois server
    */ 
    $f fsockopen($whois43$errno$errstr30); 
      if (!
    $f) { 
         echo 
    "Connection To Server Failed ($errno)"
      } else {
    /*
    Send the domain to the server and return the
    results
    */ 
         
    fputs($f"$url&#92;r&#92;n"); 
         print 
    "&lt;pre&gt;&#92;r&#92;n"
       while (!
    feof($f)) { 
            echo 
    fread($f,128); 
         } 
        print 
    "&lt;/pre&gt;";
    /*
    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.

    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 5 - Follow our Sitemap