问题描述
我知道这听起来有点奇怪,但我需要将一些参数传递给 $_POST 数组.类似于 Apache 或任何其他网络服务器的方式.
I know this could sound a little weird, but I need to pass some parameters to the $_POST array. Similar to the way Apache does it, or any other web server.
不幸的是,我在任何地方都找不到 libapache2-mod-php5
用于我的 Ubuntu 安装.
Unfortunately I couldn't find libapache2-mod-php5
anywhere for my Ubuntu installation.
推荐答案
这并不容易.您可以调用 php-cgi
二进制文件并通过管道将假 POST 请求传入.但您需要设置大量 CGI 环境变量:
That's not easily doable. You can invoke the php-cgi
binary and pipe a fake POST request in. But you'll need to set up a whole lot of CGI environment variables:
echo 'var1=123&var2=abc' | REQUEST_METHOD=POST SCRIPT_FILENAME=script.php REDIRECT_STATUS=CGI CONTENT_TYPE=application/www-form-urlencoded php-cgi
注意:不足,不能那样工作.但类似的事情...
Note: Insufficient, doesn't work like that. But something like that...
如果您只是修补脚本,并让它从预定义的环境变量中加载 $_POST 数组,那肯定会更容易.
It's certainly easier if you just patch the script, and let it load the $_POST array from a predefined environment variable.
$_POST = parse_url($_SERVER["_POST"]);
为了简单起见,您可以像 _POST=var=123 php script.php
一样调用它.
Then you can invoke it like _POST=var=123 php script.php
for simplicity.
这篇关于如何在 PHP 脚本中将参数从命令行传递到 $_POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!