将数据传输到命令行 php 中?
是否可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
PHP 可以从标准输入读取,并且还为其提供了一个很好的快捷方式:
STDIN
。有了它,您可以使用诸如
stream_get_contents
和其他人执行以下操作:这会将所有管道数据转储到
$data
中。如果您想在读取所有数据之前开始处理,或者输入大小太大而无法放入变量中,您可以使用:
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: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:
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.
据我了解,
$argv
将显示程序的参数,换句话说:但是如果将数据通过管道传输到 PHP,则必须从标准输入读取它。我从未尝试过这个,但我认为是这样的:
As I understand it,
$argv
will show the arguments of the program, in other words: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:
如果您的
data
位于类似的文件上,您还可以使用 -F 或 -R 标志(-F 读取并执行其后面的文件,-R 按字面意思执行它)如果您使用这些标志已通过管道传入的字符串将出现在(常规)全局变量 $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 $argnSimple example:
您可以通过管道输入数据,是的。但它不会出现在
$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, includingfopen('php://stdin','r')
There are good examples in the manual
这对我有用:
This worked for me:
看到这篇文章,希望制作一个行为类似于 shell 脚本的脚本,为输入的每一行执行另一个命令...例如:
如果您希望制作一个行为类似的 php 脚本,这适用于我:
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:
If you're looking to make a php script that behaves in a similar way, this worked for me:
如果您希望它显示在 $argv 中,请尝试以下操作:
这会将标准输入中的所有内容隐藏到命令行参数中。
If you want it to show up in
$argv
, try this:That would covert whatever goes into standard input into command line arguments.
最好的选择是使用 -r 选项并从标准输入中获取数据。即我使用它可以使用 PHP 轻松解码 JSON。
这样您就不必创建物理脚本文件。
它是这样的:
这段代码将显示主机和主机之间的共享文件夹。一个容器。
请将 $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:
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() { ... }
我需要获取 CSV 文件并将其转换为 TSV 文件。当然,我可以将文件导入 Excel,然后重新导出,但是通过转换器传输数据意味着我可以留在命令行中轻松完成工作,这有什么乐趣!
因此,我的脚本(称为 csv2tsv)是
chmod +x csv2tsv。
然后我可以运行它
cat data.csv | csv2tsv > data.tsv
现在我的数据已成为 TSV!好的。没有错误检查(数据是实际的 CSV 文件吗?)等,但原理运行良好。
当然,您可以根据需要链接任意数量的命令。
如果您想要更多地扩展这个想法,那么在您的命令中包含其他选项的能力怎么样?
简单的!
现在我可以将默认分隔符从制表符覆盖为其他内容。也许是
|
!猫数据.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
) isI
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!
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!
它也可以由
STDIN
提供。将文件送入 STDIN:
但是,将字符串传递到 shell 会导致某些 Unicode 字符出现问题,并可能导致与 shell 相关的烦恼。为了避免这种情况,请使用以
base64
编码的字符串:It can be fed by the
STDIN
as well.Feeding a file into the STDIN:
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
: