如何将参数从命令行传递到php-script中的$_POST ?

时间:2021-02-19 01:47:30

I know this could sound a little weird but I need to pass some parameters to a $_POST array. Similar to the way apache does it, or any other web server.

我知道这听起来可能有点奇怪,但我需要向$_POST数组传递一些参数。类似于apache或任何其他web服务器的处理方式。

Unfortunately I couldn't find libapache2-mod-php5 anywhere for my Ubuntu.

不幸的是,我在Ubuntu的任何地方都找不到libapache2-mod-php5。

3 个解决方案

#1


16  

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:

这不是容易可行。您可以调用php-cgi二进制文件并导入伪POST请求。但是您需要设置大量的CGI环境变量:

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...

注意:不够,不是那样工作的。但这样的…


It's certainly easier if you just patch the script, and let it load the $_POST array from a predefined environment variable.

如果只是对脚本进行补丁,并让它从预定义的环境变量加载$_POST数组,当然会更容易。

$_POST = parse_url($_SERVER["_POST"]);

Then you can invoke it like _POST=var=123 php script.php for simplicity.

然后可以调用它,比如_POST=var=123 php脚本。php为简单起见。

#2


40  

I was searching for a solution for this and came by, because it was the first hit at Google. The second one was somehow mor useful for me, because it has a really easy solution, if you have access to the PHP script and can change it.

我一直在寻找这个问题的解决方案,然后就来了,因为它是谷歌的第一个热门。第二种方法对我来说是有用的,因为它有一个非常简单的解决方案,如果你可以访问PHP脚本并且可以修改它。

Just insert the following lines at the beginning of your script:

只需在脚本的开头插入以下几行:

/* if started from commandline, wrap parameters to $_POST and $_GET */
if (!isset($_SERVER["HTTP_HOST"])) {
  parse_str($argv[1], $_GET);
  parse_str($argv[1], $_POST);
}

This small piece of code does the trick (you may decide if you want to use $_GET or $_POST or, like I needed it, both.
After changing your script you can call it from commandline passing your args:

这一小段代码可以起到这个作用(您可以决定是否要使用$_GET或$_POST,或者像我需要的那样,两者都要使用。修改脚本后,您可以从命令行调用它,并通过args:

php yourscript.php 'arg1=x&arg2=y'

Have fun!

玩得开心!

#3


3  

use curl to post data
curl --data "name=ii" "param1=value1&param2=value2" http://test.com/sample.php

使用curl来发布数据curl——data "name=ii" "param1=value1&param2=value2" http://test.com/sample.php。

#1


16  

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:

这不是容易可行。您可以调用php-cgi二进制文件并导入伪POST请求。但是您需要设置大量的CGI环境变量:

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...

注意:不够,不是那样工作的。但这样的…


It's certainly easier if you just patch the script, and let it load the $_POST array from a predefined environment variable.

如果只是对脚本进行补丁,并让它从预定义的环境变量加载$_POST数组,当然会更容易。

$_POST = parse_url($_SERVER["_POST"]);

Then you can invoke it like _POST=var=123 php script.php for simplicity.

然后可以调用它,比如_POST=var=123 php脚本。php为简单起见。

#2


40  

I was searching for a solution for this and came by, because it was the first hit at Google. The second one was somehow mor useful for me, because it has a really easy solution, if you have access to the PHP script and can change it.

我一直在寻找这个问题的解决方案,然后就来了,因为它是谷歌的第一个热门。第二种方法对我来说是有用的,因为它有一个非常简单的解决方案,如果你可以访问PHP脚本并且可以修改它。

Just insert the following lines at the beginning of your script:

只需在脚本的开头插入以下几行:

/* if started from commandline, wrap parameters to $_POST and $_GET */
if (!isset($_SERVER["HTTP_HOST"])) {
  parse_str($argv[1], $_GET);
  parse_str($argv[1], $_POST);
}

This small piece of code does the trick (you may decide if you want to use $_GET or $_POST or, like I needed it, both.
After changing your script you can call it from commandline passing your args:

这一小段代码可以起到这个作用(您可以决定是否要使用$_GET或$_POST,或者像我需要的那样,两者都要使用。修改脚本后,您可以从命令行调用它,并通过args:

php yourscript.php 'arg1=x&arg2=y'

Have fun!

玩得开心!

#3


3  

use curl to post data
curl --data "name=ii" "param1=value1&param2=value2" http://test.com/sample.php

使用curl来发布数据curl——data "name=ii" "param1=value1&param2=value2" http://test.com/sample.php。