13.4 Working with Sockets


You want to open a socket to either a local port or a remote server.

Technique

Use the fsockopen() function, which opens a socket to the specified server. Subsequently, you send to and retrieve information from that socket via the fgets() , fgetss() , and fputs() functions:

 <?php $sp = fsockopen($hostname, $port, &$errno, &$errstr, $timeout); if (!$sp) {     print "$errstr  [$errno]<br>\n"; } else {     fputs($fp,"GET / HTTP/1.0\r\n\r\n");     while ($line = fgets($fp, 1024)) {         print $line;     } fclose($sp); } ?> 

Comments

The fsockopen() function provides a simple API for using sockets. For more information on fsockopen() and some of PHP's more advanced socket features, see Chapter 14, "Communicating with Sockets."



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net