Your last example uses the -c
parameter, from the man page:
-c
Look for php.ini file in the directory path or use the specified file
I guess you ment to use -r
:
-r
Run PHP code without using script tags '<?..?>'
In your answer, the HELLO
comes from the shell, if you replace -c
with -r
is shows nothing, for that, please continue reading
The above seen in shell, on my local machine, in a snippet because the answer is getting way to long
$
$ echo 'HELLO' | php -c "echo 'Test'; echo file_get_contents('php://input');"
HELLO
$
$ # HELLO Comes from shell, no 'echo 'Test';
$
$ echo 'HELLO' | php -r "echo 'Test'; echo file_get_contents('php://input');"
Test%
$
$ # With `-r` only the Test echo, because php:// input is empty
$
$
$ echo 'HELLO' | php -r "echo 'Test'; echo file_get_contents('php://stdin');"
TestHELLO
$
$ # If we use php://stdin we get both the echo's
$
From the documentation:
php://input
is a read-only stream that allows you to read raw data from the request body
Your data is in stdin
, so you'll need file_get_contents('php://stdin')
:
php://stdin
, php://stdout
and php://stderr
allow direct access to the corresponding input or output stream of the PHP process.
Source of above quotes
<?php
$stdin = file_get_contents('php://stdin');
echo ">>>>>";
echo $stdin;
echo "<<<<<";
Another option is to use stream_get_contents(STDIN)
:
<?php
$stdin = stream_get_contents(STDIN);
print(">>>>>");
print($stdin);
print("<<<<<");