Recipe 23.4. Reading from Standard Input


23.4.1. Problem

You want to read from standard input in a command-line context'for example, to get user input from the keyboard or data piped to your PHP program.

23.4.2. Solution

Use fopen( ) to open php://stdin, as in Example 23-15.

Reading from standard input

<?php $fh = fopen('php://stdin','r') or die($php_errormsg); while($s = fgets($fh)) {     print "You typed: $s"; } ?>

23.4.3. Discussion

Recipe 25.3 discusses reading data from the keyboard in a command-line context in more detail. Reading data from standard input isn't very useful in a web context, because information doesn't arrive via standard input. The bodies of HTTP post and file-upload requests are parsed by PHP and put into special variables. Non-file-upload post request bodies can also be read with the php://input stream, as discussed in Recipe 8.7.

23.4.4. See Also

Recipe 25.3 for reading from the keyboard in a command-line context; Recipe 8.7 for reading POST request bodies; documentation on fopen( ) at http://www.php.net/fopen.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

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