How can I pass parameters from the command line to $_POST in a PHP script?(如何在 PHP 脚本中将参数从命令行传递到 $_POST?)
问题描述
我知道这听起来有点奇怪,但我需要将一些参数传递给 $_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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PHP 脚本中将参数从命令行传递到 $_POST?


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01