note 25370 deleted from ref.sockets by nlopess

From: Date: Thu, 11 Dec 2003 14:20:31 +0000
Subject: note 25370 deleted from ref.sockets by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-61800@lists.php.net to get a copy of this message
Note Submitter: gv (at) damnsw (dot) net ---- #!/usr/bin/php -q <?php /* This is a remake of a simple, one-shot HTTP client which is more or less nonfunctional with the current PHP sockets implementation. The original can currently be found in the 'examples' section of the main sockets documentation -- http://http://www.php.net/manual/en/ref.sockets.php Use by chmod 755'ing it, then running in the command line: ./thisfile.php This example requires the sockets extension compiled into the PHP Commandline Module (http://www.php.net/manual/en/features.commandline.php) to run without modification. It can, theoretically, be used with the web service module (Remove the "#!/usr/bin/php -q" line), but that would reduce the already minimal functional use to about nill. Of course, it will function as an example of working sockets regardless. */ error_reporting (E_ALL); /* Get the port for the WWW service. This could be prevented by just using '80' in the socket_connect() call, but this is a slightly more human-readable method, or something. */ $service_port = getservbyname( "www", "tcp" ); /* This part is actually rather important -- socket_connect() takes an IP, not a hostname. */ $address = gethostbyname( "www.example.com" ); /* Create a TCP/IP socket. */ $socket = socket_create( AF_INET, SOCK_STREAM, 0 ); if( $socket < 0 ) { echo "socket_create() failed: reason: " . socket_strerror( $socket ) . "\n"; exit; /* There's no reason to continue to execute the script -- adding intelligent retry code would be beyond the scope of the original example. */ } /* Attempt to connect to $address on $service_port */ if( !( @socket_connect( $socket, $address, $service_port ) ) ) { /* Print an error message if the socket failed to connect; supress PHP's builtin error message. */ $result = socket_last_error( $socket ); socket_clear_error( $socket ); print "Error connecting to $ip:$port -- " . socket_strerror( $result ) . " ($result)\n"; exit; /* Once again, we're not going to bother retrying. */ } $in = "GET / HTTP/1.0\r\n\r\n"; $out = ''; /* Send the request for data. */ socket_write( $socket, $in, strlen( $in ) ); /* Read data. */ while( ($out = socket_read( $socket, 2048, PHP_BINARY_READ ) ) != false ) { /* As can be seen in the socket_read() documentation, the optional "type" (in this case, PHP_BINARY_READ) determines when socket_read() stops reading. The binary read type will read until there's no more data or the limit has been reached -- PHP_NORMAL_READ will stop at \r or \n. Doing a normal read in this situation would complicate matters because we'd need to re-add the line breaks. While that may sound easy, correctly guessing if we're at the end of the line or we're just seeing the read limit kicking in would be a little iffy. Besides, there are no disadvantages to the binary read. */ echo $out; } /* Theoretically, the webserver has already closed the socket, as it has finished sending data. However, it doesn't hurt to socket_close(), as it does deallocational stuff that will not happen automagically when the remote side dies. Presumably, PHP will do this anyway when the script ends, but it doesn't hurt. */ socket_close( $socket ); /* *sniffle* My poor, innocent indenting. Waah. A better formatted copy can be found here: http://www.yghe.net/~skunks/sockets.phps */ ?>

« previous php.notes (#61800) next »