0

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:

user3665736
  • 101
  • 1
  • 6

3 Answers3

5

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

Gino Pane
  • 4,740
  • 5
  • 31
  • 46
  • Thanks for your answer, in fact I have many php scripts using file_get_contents("php://input") and I try to figure out how to pass the info in command line – user3665736 Feb 21 '19 at 12:58
  • 1
    php://input is for request body, in command line you'll have standard input: http://php.net/manual/en/features.commandline.io-streams.php – Gino Pane Feb 21 '19 at 13:01
  • Yes I want to simulate a request body (some JSON string in fact) – user3665736 Feb 21 '19 at 13:10
0

You can use the arguments.

#!/usr/bin/php
<?php
// loop through each element in the $argv array
foreach($argv as $value){
  echo "$value\n";
}

Vidal
  • 2,605
  • 2
  • 16
  • 32
0

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"));'

user3665736
  • 101
  • 1
  • 6