PHP
  Home arrow PHP arrow Page 7 - Socket Programming With PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Socket Programming With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 159
    2002-02-05


    Table of Contents:
  • Socket Programming With PHP
  • Putting It All Together
  • Fortune's Fool
  • Looping The Loop
  • On Web-bed Feet
  • Different Strokes
  • POP Goes The Weasel
  • Access Denied
  • Game Over

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Socket Programming With PHP - POP Goes The Weasel
    ( Page 7 of 9 )

    Let's try something a little more advanced. How about a TCP client that connects to a POP3 server to retrieve the total number of messages for a user's mailbox?

    In order to build such a client, I need to first understand the sequence of commands passed to a POP3 server in order to obtain the message total, and then replicate this sequence in my PHP-based client. The best way to do this is by using a regular telnet client to interact with the server and understand the command sequence - so here goes:

    $ telnet mail.host 110 Trying 192.168.0.1... Connected to 192.168.0.1. Escape character is '^]'. +OK POP3 mail.host v5.5 server ready USER john +OK User name accepted, password please PASS doe +OK Mailbox open, 72 messages STAT +OK 72 24595628 QUIT +OK Sayonara Connection closed by foreign host.
    As you can see from the sample session above, the second element of the string returned by a STAT command

    STAT +OK 72 24595628
    holds the total number of messages (72, in this case). All that's needed, therefore, is a script that connects to the POP3 server (usually available on port 110), sends the sequence of commands above, retrieves the output of the STAT command, and extracts the message total from it.

    Here's the script to accomplish this:

    <? // mail server settings $host="192.168.0.99"; $port = 110; $user = "john"; $pass = "doe"; // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); // if a handle is not returned if (!$fp) { die("Error: could not open socket connection\n"); } else { // get the welcome message $welcome = fgets ($fp, 150); // check for success code if (substr($welcome, 0, 3) == "+OK") { // send username and read response fputs ($fp, "USER $user\n"); fgets($fp, 50); // send password and read response fputs ($fp, "PASS $pass\n"); $ack = fgets($fp, 50); // check for success code if (substr($ack, 0, 3) == "+OK") { // send status request and read response fputs ($fp, "STAT\n"); $status = fgets($fp, 50); if (substr($status, 0, 3) == "+OK") { // shut down connection fputs ($fp, "QUIT\n"); fclose ($fp); } // error getting status else { die ("Server said: $status"); } } // auth failure else { die ("Server said: $ack"); } } // bad welcome message else { die ("Bad connection string\n"); } // get status string // split by spaces $arr = explode(" ", $status); // the second element contains the total number of messages echo $arr[1] . " messages in mailbox"; } ?>
    And here's the output:

    $ /usr/local/bin/php -q popclient.php 72 messages in mailbox
    How does this work? Very simple.

    First, a connection is opened to the POP3 server using the fsockopen() function discussed previously; the arguments to this function (host, port et al) are obtained through PHP variables which are set at the top of the script.

    <? // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); ?>
    Once a socket connection has been established, the fgets() and fputs() functions are used to send POP3 commands to the server via this socket, and read the resulting output into PHP variables.

    <? // send username and read response fputs ($fp, "USER $user\n"); fgets($fp, 50); // send password and read response fputs ($fp, "PASS $pass\n"); $ack = fgets($fp, 50); ?>
    A POP3 server typically prefixes the result of every successful command with the string "+OK". This knowledge allows for simple error-checking within the script - note how the output of fgets() is checked at every stage, with subsequent commands executed only if the previous one was successful.

    <? // send status request and read response fputs ($fp, "STAT\n"); $status = fgets($fp, 50); if (substr($status, 0, 3) == "+OK") { // shut down connection fputs ($fp, "QUIT\n"); fclose ($fp); } // error getting status else { die ("Server said: $status"); } ?>
    Once the output of the STAT command has been received, the socket is closed, and the result string is split into its constituent parts with PHP's very cool explode() function. The message total is then extracted and printed.

    <? // get status string // split by spaces $arr = explode(" ", $status); // the second element contains the total number of messages echo $arr[1] . " messages in mailbox"; ?>
    Simple, huh?

     
     
    >>> More PHP Articles          >>> More By icarus, (c) Melonfire
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT