从命令行将数据放入php://input

7

我有一个小的PHP脚本,它从php://input读取数据。我可以在命令行中运行该脚本,但我不知道如何“填充”php://input

我尝试使用php file.php < my_test_data,但它填充了php://stdin而不是php://input

这个脚本可以概括为:

<?php echo file_get_contents('php://input'); ?>

我认为这是不可能的;php://input$_POST请求中获取数据(你无法在命令行中填充它 - 除非你使用类似于wgetcurl的工具进行真实请求)。 - raina77ow
1个回答

8

php://input 只适用于从 Web 服务器运行的脚本。

当 CLI 脚本需要访问标准输入时,它们使用 php://stdin,或者已经打开的流 STDIN

<?php echo file_get_contents('php://stdin'); ?>

或者

<?php echo stream_get_contents(STDIN); ?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接