Reputation: 101
Not a dupe, in this very case I am trying to run my php thru a command line and it does not work as expected (php uses php://input and I do not want to modify them)
I am trying the following:
echo 'hello' | php -r 'print("input content:".file_get_contents("php://input"));'
I am expecting something like
content:hello
to be printed out , but get only
content:
Upvotes: 0
Views: 74
Reputation: 101
In fact php://input will only work if I use php-cgi instead of php So to pass a request body code is like:
echo '{"a":1}' | php-cgi -r 'print("input content:".file_get_contents("php://input"));'
Upvotes: 0
Reputation: 5011
Please try with stdin
instead:
echo "hello" | php -r 'print_r("input content:".file_get_contents("php://stdin"));'
More about CLI streams: http://php.net/manual/en/features.commandline.io-streams.php
Upvotes: 5
Reputation: 2606
You can use the arguments.
#!/usr/bin/php
<?php
// loop through each element in the $argv array
foreach($argv as $value){
echo "$value\n";
}
Upvotes: 0