user3665736
user3665736

Reputation: 101

PHP : how to retrieve piped stuff

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

Answers (3)

user3665736
user3665736

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

Gino Pane
Gino Pane

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

Vidal
Vidal

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

Related Questions