piping data into command line php?

2019-03-18 02:08发布

It is possible to pipe data using unix pipes into a command-line php script? I've tried

$> data | php script.php

But the expected data did not show up in $argv. Is there a way to do this?

8条回答
叼着烟拽天下
2楼-- · 2019-03-18 02:44

Best option is to use -r option and take the data from the stdin. Ie I use it to easily decode JSON using PHP. This way you don't have to create physical script file.

It goes like this:

docker inspect $1|php -r '$a=json_decode(stream_get_contents(STDIN),true);echo str_replace(["Array",":"],["Shares","  -->  "],print_r($a[0]["HostConfig"]["Binds"],true));'

This piece of code will display shared folders between host & a container. Please replace $1 by the container name or put it in a bash alias like ie displayshares() { ... }

查看更多
The star\"
3楼-- · 2019-03-18 02:57

You can pipe data in, yes. But it won't appear in $argv. It'll go to stdin. You can read this several ways, including fopen('php://stdin','r')

There are good examples in the manual

查看更多
登录 后发表回答