Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - 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 - Sending The Request


    (Page 3 of 6 )

    In the following section you will learn how to send a request to a server and then list how many lines the server returned for a particular page and then how to loop through the returned array to display the page. Once you have a socket open to a server the variable $fp or whatever you have called it acts like a file in many ways, meaning you can send variables to $fp and return results.

    <?php
    /*
    Once again we connect to the server at example.com
    */
    $host "www.example.com";
    $page "/index.html";
    $fp fsockopen($host80$errno$errdesc) or
    die(
    "Connection to $host failed");
    /*
    Now we define the headers to be sent to the server
    GET means we want the page or we want the contents
    of it. We can use POST to send variables to that
    page and return the results as i will show you
    later in this tutorial.
    */
    $request "GET $page HTTP/1.0&#92;r&#92;n";
    $request .= "Host: $host&#92;r&#92;n";
    $request .= "Referer: $host&#92;r&#92;n";
    /*
    Using fputs() we send the request to the server
    and then loop through the results and form an
    array called $page
    */
    fputs($fp$request);
    while(!
    feof($fp)){
    $page[] = fgets($fp1024);
    }
    /*
    Close the connection and count how many lines the
    server returned for a certain page
    */
    fclose($fp);
    echo 
    "The server returned ".(count($page)).
    " Lines";
    /*
    Loop through the page array and print each line to
    the browser. Here we use the for() statement.
    */
    for($i=0$i&lt;count($page); $i++){
    echo 
    $page[$i];
    }
    ?>

    That shouldn't of been too hard because after the initial connection and when we returned the array with the request we only looped through the array and printed its contents to the browser. In the next section I will show you a example of connecting to multiple servers.

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