将数据传输到命令行 php 中?

发布于 2024-11-05 04:18:01 字数 165 浏览 5 评论 0原文

是否可以使用 unix 管道将数据传输到命令行 php 脚本中?我已经尝试过

$> data | php script.php

,但预期的data没有出现在$argv中。有办法做到这一点吗?

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

gt; data | php script.php

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

无名指的心愿 2024-11-12 04:18:01

PHP 可以从标准输入读取,并且还为其提供了一个很好的快捷方式: STDIN

有了它,您可以使用诸如 stream_get_contents 和其他人执行以下操作:

$data = stream_get_contents(STDIN);

这会将所有管道数据转储到 $data 中。

如果您想在读取所有数据之前开始处理,或者输入大小太大而无法放入变量中,您可以使用:

while(!feof(STDIN)){
    $line = fgets(STDIN);
}

STDIN 只是 $fh = fopen( “php://stdin”,“r”);
相同的方法可以应用于读写文件和 TCP 流。

PHP can read from standard input, and also provides a nice shortcut for it: STDIN.

With it, you can use things like stream_get_contents and others to do things like:

$data = stream_get_contents(STDIN);

This will just dump all the piped data into $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.

逐鹿 2024-11-12 04:18:01

据我了解, $argv 将显示程序的参数,换句话说:

php script.php arg1 arg2 arg3

但是如果将数据通过管道传输到 PHP,则必须从标准输入读取它。我从未尝试过这个,但我认为是这样的:

$fp = readfile("php://stdin");
// read $fp as if it were a file

As I understand it, $argv will show the arguments of the program, in other words:

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:

$fp = readfile("php://stdin");
// read $fp as if it were a file
二智少女猫性小仙女 2024-11-12 04:18:01

如果您的 data 位于类似的文件上,您还可以使用 -F 或 -R 标志(-F 读取并执行其后面的文件,-R 按字面意思执行它)如果您使用这些标志已通过管道传入的字符串将出现在(常规)全局变量 $argn 中

简单示例:

echo "hello world" | php -R 'echo str_replace("world","stackoverflow", $argn);'

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

Simple example:

echo "hello world" | php -R 'echo str_replace("world","stackoverflow", $argn);'
月亮是我掰弯的 2024-11-12 04:18:01

您可以通过管道输入数据,是的。但它不会出现在$argv中。它将转到标准输入。您可以通过多种方式阅读此内容,包括 fopen('php://stdin','r')

有很好的示例 在手册中

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

荆棘i 2024-11-12 04:18:01

这对我有用:

stream_get_contents(fopen("php://stdin", "r"));

This worked for me:

stream_get_contents(fopen("php://stdin", "r"));
蓝眼睛不忧郁 2024-11-12 04:18:01

看到这篇文章,希望制作一个行为类似于 shell 脚本的脚本,为输入的每一行执行另一个命令...例如:

ls -ln | awk '{print $9}'

如果您希望制作一个行为类似的 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;
}

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:

ls -ln | awk '{print $9}'

If you're looking to make a php script that behaves in a similar way, this worked for me:

#!/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;
}
终难愈 2024-11-12 04:18:01

如果您希望它显示在 $argv 中,请尝试以下操作:

echo "Whatever you want" | xargs php script.php

这会将标准输入中的所有内容隐藏到命令行参数中。

If you want it to show up in $argv, try this:

echo "Whatever you want" | xargs php script.php

That would covert whatever goes into standard input into command line arguments.

云胡 2024-11-12 04:18:01

最好的选择是使用 -r 选项并从标准输入中获取数据。即我使用它可以使用 PHP 轻松解码 JSON。
这样您就不必创建物理脚本文件。

它是这样的:

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

这段代码将显示主机和主机之间的共享文件夹。一个容器。
请将 $1 替换为容器名称或将其放入 bash 别名中,例如 ie displayshares() { ... }

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() { ... }

流星番茄 2024-11-12 04:18:01

我需要获取 CSV 文件并将其转换为 TSV 文件。当然,我可以将文件导入 Excel,然后重新导出,但是通过转换器传输数据意味着我可以留在命令行中轻松完成工作,这有什么乐趣!

因此,我的脚本(称为 csv2tsv)是

#!/usr/bin/php
<?php
while(!feof(STDIN)){
    echo implode("\t", str_getcsv(fgets(STDIN))), PHP_EOL;
}

chmod +x csv2tsv。

然后我可以运行它 cat data.csv | csv2tsv > data.tsv 现在我的数据已成为 TSV!

好的。没有错误检查(数据是实际的 CSV 文件吗?)等,但原理运行良好。

当然,您可以根据需要链接任意数量的命令。

如果您想要更多地扩展这个想法,那么在您的命令中包含其他选项的能力怎么样?

简单的!

#!/usr/bin/php
<?php
$separator = $argv[1] ?? "\t";
while(!feof(STDIN)){
    echo implode($separator, str_getcsv(fgets(STDIN))), PHP_EOL;
}

现在我可以将默认分隔符从制表符覆盖为其他内容。也许是 |

猫数据.csv | csv2tsv“|” > data.psv

希望这对您有所帮助,并让您看到您还可以做多少事情!

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!

So, my script (called csv2tsv) is

#!/usr/bin/php
<?php
while(!feof(STDIN)){
    echo implode("\t", str_getcsv(fgets(STDIN))), PHP_EOL;
}

I chmod +x csv2tsv.

I can then run it cat data.csv | csv2tsv > data.tsv and I now have my data as a TSV!

OK. No error checking (is the data an actual CSV file?), etc. but the principle works well.

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

Hope this helps and allows you to see how much more you can do!

抱猫软卧 2024-11-12 04:18:01

它也可以由 STDIN 提供。

php <<< 'Hi <?php echo getenv()["USER"];?>!'

echo 'Hi <?php echo getenv()["USER"];?>!' | php

将文件送入 STDIN:

cat code.php | php

php <<< $(cat somecool.php)

php < somecool.php

但是,将字符串传递到 shell 会导致某些 Unicode 字符出现问题,并可能导致与 shell 相关的烦恼。为了避免这种情况,请使用以 base64 编码的字符串:

system("echo '$base64String' | base64 --decode | php");

It can be fed by the STDIN as well.

php <<< 'Hi <?php echo getenv()["USER"];?>!'

echo 'Hi <?php echo getenv()["USER"];?>!' | php

Feeding a file into the STDIN:

cat code.php | php

php <<< $(cat somecool.php)

php < somecool.php

However passing strings to the shell will cause issues with some Unicode characters and may cause shell related annoyances. To avoid that, use a string encoded in base64:

system("echo '$base64String' | base64 --decode | php");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文