php 管道数据到命令行php?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5891888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
piping data into command line php?
提问by user151841
It is possible to pipe data using unix pipes into a command-line php script? I've tried
是否可以使用 unix 管道将数据通过管道传输到命令行 php 脚本中?我试过了
$> data | php script.php
But the expected data
did not show up in $argv
. Is there a way to do this?
但预期中data
并没有出现$argv
。有没有办法做到这一点?
采纳答案by Gustav Larsson
As I understand it, $argv
will show the arguments of the program, in other words:
据我了解,$argv
将显示程序的参数,换句话说:
php script.php arg1 arg2 arg3
But if you pipe data into PHP, you will have to read it from standard input. I've never tried this, but I think it's something like this:
但是如果将数据通过管道传输到 PHP,则必须从标准输入中读取它。我从未尝试过,但我认为它是这样的:
$fp = readfile("php://stdin");
// read $fp as if it were a file
回答by hawken
PHP can read from standard input, and also provides a nice shortcut for it: STDIN
.
PHP 可以从标准输入中读取,并且还提供了一个很好的快捷方式:STDIN
.
With it, you can use things like stream_get_contents
and others to do things like:
有了它,你可以使用像stream_get_contents
和其他的东西来做这样的事情:
$data = stream_get_contents(STDIN);
This will just dump all the piped data into $data
.
这只会将所有管道数据转储到$data
.
If you want to start processing before all data is read, or the input size is too big to fit into a variable, you can use:
如果要在读取所有数据之前开始处理,或者输入大小太大而无法放入变量,可以使用:
while(!feof(STDIN)){
$line = fgets(STDIN);
}
STDIN
is just a shortcut of $fh = fopen("php://stdin", "r");
.
The same methods can be applied to reading and writing files, and tcp streams.
STDIN
只是一个快捷方式$fh = fopen("php://stdin", "r");
。相同的方法可以应用于读写文件和 tcp 流。
回答by giblfiz
If your data
is on one like, you can also use either the -F or -R flag (-F reads & executes the file following it, -R executes it literally) If you use these flags the string that has been piped in will appear in the (regular) global variable $argn
如果您data
喜欢,您还可以使用 -F 或 -R 标志(-F 读取并执行其后的文件,-R 按字面意思执行)如果您使用这些标志,则已通过管道输入的字符串将出现在(常规)全局变量 $argn 中
Simple example:
简单的例子:
echo "hello world" | php -R 'echo str_replace("world","stackoverflow", $argn);'
回答by Frank Farmer
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')
您可以通过管道输入数据,是的。但它不会出现在$argv
. 它将转到标准输入。您可以通过多种方式阅读本文,包括fopen('php://stdin','r')
There are good examples in the manual
手册中有很好的例子
回答by klokop
This worked for me:
这对我有用:
stream_get_contents(fopen("php://stdin", "r"));
回答by stephenspann
Came upon this post looking to make a script that behaves like a shell script, executing another command for each line of the input... ex:
看到这篇文章,希望制作一个行为类似于 shell 脚本的脚本,为输入的每一行执行另一个命令......例如:
ls -ln | awk '{print }'
If you're looking to make a php script that behaves in a similar way, this worked for me:
如果您想制作一个行为类似的 php 脚本,这对我有用:
#!/usr/bin/php
<?php
$input = stream_get_contents(fopen("php://stdin", "r"));
$lines = explode("\n", $input);
foreach($lines as $line) {
$command = "php next_script.php '" . $line . "'";
$output = shell_exec($command);
echo $output;
}
回答by user914330
If you want it to show up in $argv
, try this:
如果您希望它出现在 中$argv
,请尝试以下操作:
echo "Whatever you want" | xargs php script.php
That would covert whatever goes into standard input into command line arguments.
这会将进入标准输入的任何内容转换为命令行参数。
回答by Nadir
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.
最好的选择是使用 -r 选项并从标准输入中获取数据。即我用它来轻松地使用 PHP 解码 JSON。这样您就不必创建物理脚本文件。
It goes like this:
它是这样的:
docker inspect |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() { ... }
这段代码将显示主机和容器之间的共享文件夹。请用容器名称替换 $1 或将其放在 bash 别名中,例如 ie displayshares() { ... }
回答by Richard A Quadling
I needed to take a CSV file and convert it to a TSV file. Sure, I could import the file into Excel and then re-export it, but where's the fun in that when piping the data through a converter means I can stay in the commandline and get the job done easily!
我需要获取一个 CSV 文件并将其转换为 TSV 文件。当然,我可以将文件导入 Excel 然后重新导出它,但是当通过转换器传输数据时,这意味着我可以留在命令行中轻松完成工作,这有什么乐趣呢!
So, my script (called csv2tsv
) is
所以,我的脚本(称为csv2tsv
)是
#!/usr/bin/php
<?php
while(!feof(STDIN)){
echo implode("\t", str_getcsv(fgets(STDIN))), PHP_EOL;
}
I chmod +x csv2tsv
.
我chmod +x csv2tsv
。
I can then run it cat data.csv | csv2tsv > data.tsv
and I now have my data as a TSV!
然后我可以运行它cat data.csv | csv2tsv > data.tsv
,我现在将我的数据作为 TSV!
OK. No error checking (is the data an actual CSV file?), etc. but the principle works well.
好的。没有错误检查(数据是实际的 CSV 文件吗?)等,但原理运行良好。
And of course, you can chain as many commands as you need.
当然,您可以根据需要链接任意数量的命令。
If you are wanting more to expand on this idea, then how about the ability to include additional options to your command?
如果您想要更多地扩展这个想法,那么在您的命令中包含其他选项的能力如何?
Simple!
简单的!
#!/usr/bin/php
<?php
$separator = $argv[1] ?? "\t";
while(!feof(STDIN)){
echo implode($separator, str_getcsv(fgets(STDIN))), PHP_EOL;
}
Now I can overwrite the default separator from being a tab to something else. A |
maybe!
现在我可以将默认分隔符从选项卡覆盖为其他内容。一个|
也许!
cat data.csv | csv2tsv '|' > data.psv
cat data.csv | csv2tsv '|' > data.psv
Hope this helps and allows you to see how much more you can do!
希望这会有所帮助,并让您了解自己还能做多少!