Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the contents of stdin (up until EOF) into a string in PHP?

Tags:

php

stream

How can I read the contents of stdin (up until EOF) into a string in PHP?

like image 969
zak23 Avatar asked May 05 '09 05:05

zak23


People also ask

How do you read from stdin until EOF?

Use the sys. stdin. readlines() method to read user input until EOF. The readlines() method will return a list containing the lines.

How can I get the content of a file in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What is Stdin PHP?

Standard streams php://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.


2 Answers

ended up figuring it out myself:

$input_data = file_get_contents("php://stdin"); 
like image 197
zak23 Avatar answered Oct 05 '22 23:10

zak23


Here's an alternative: You can use stream_get_contents to get everything from an open stream (AKA a file handle). STDIN is an already open handle equivalent to what you get if you call fopen('php://stdin') (see reference: Input/output streams

So: you can use

$input_data = stream_get_contents(STDIN); 

which is equivalent to the accepted answer.

like image 23
bart Avatar answered Oct 05 '22 23:10

bart