What's the difference between php://input and php://stdin What's the difference between php://input and php://stdin php php

What's the difference between php://input and php://stdin


The difference is in the environment where you're expected to use them. php://stdin, php://stdout, and php://stderr are mapped directly to the relevant POSIX file streams and are intended for use with the CLI SAPI. On the other hand, php://input and php://output are intended for use with web-based SAPIs.

Try running these two commands from the command line:

printf "foo" | php -r "var_dump(file_get_contents('php://stdin'));"printf "foo" | php -r "var_dump(file_get_contents('php://input'));"

You're going to get output like this:

Command line code:1:string(3) "foo"Command line code:1:string(0) ""

Because php://input expects to be used by a web SAPI like CGI or mod_php and will not get the contents of STDIN passed to it. Likewise, trying to read raw POST data (the only real use for php://input) using php://stdin would fail.

php://output can generally be used in both environments but it's rarely used at all, since one can simply echo output. php://stdout is the more logical choice for command line code, though again it's generally easier to just use echo.

php://stderr is of course invaluable to command line programmers who need to output informational, debug, or error messages to a different stream than the program output.