PHP standard input?
It is possible to read the stdin
by creating a file handle to php://stdin
and then read from it with fgets()
for a line for example (or, as you already stated, fgetc()
for a single character):
<?php$f = fopen( 'php://stdin', 'r' );while( $line = fgets( $f ) ) { echo $line;}fclose( $f );?>
To avoid having to mess around with filehandles, use file_get_contents()
and php://stdin
:
$ echo 'Hello, World!' | php -r 'echo file_get_contents("php://stdin");'Hello, World!
(If you're reading a truly huge amount of data from stdin
you might want to use the filehandle approach, but this should be good for many megabytes.)