PHP在Linux命令提示符中传递$ _GET [英] PHP passing $_GET in linux command prompt

查看:242
本文介绍了PHP在Linux命令提示符中传递$ _GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们通常通过

http://localhost/index.php?a=1&b=2&c=3

我们如何在linux命令提示符下执行相同的操作?

How do we execute the same in linux command prompt?

php -e index.php

但是如何传递$ _GET变量呢?也许像php -e index.php --a 1 --b 2 --c 3之类的东西?怀疑会起作用.

But what about passing the $_GET variables? Maybe something like php -e index.php --a 1 --b 2 --c 3? Doubt that'll work.

谢谢!

推荐答案

通常,为了将参数传递给命令行脚本,您将使用argv全局变量或

Typically, for passing arguments to a command line script, you will use either argv global variable or getopt:

// bash command:
//   php -e myscript.php hello
echo $argv[1]; // prints hello

// bash command:
//   php -e myscript.php -f=world
$opts = getopt('f:');
echo $opts['f']; // prints world

$ _ GET是HTTP GET方法的参数,在命令行中不可用,因为它们需要Web服务器来填充.

$_GET refers to the HTTP GET method parameters, which are unavailable in command line, since they require a web server to populate.

如果您仍然想填充$ _GET,则可以执行以下操作:

If you really want to populate $_GET anyway, you can do this:

// bash command:
//   export QUERY_STRING="var=value&arg=value" ; php -e myscript.php
parse_str($_SERVER['QUERY_STRING'], $_GET);
print_r($_GET);
/* outputs:
     Array(
        [var] => value
        [arg] => value
     )
*/

您还可以执行给定的脚本,从命令行填充$_GET,而无需修改该脚本:

You can also execute a given script, populate $_GET from the command line, without having to modify said script:

export QUERY_STRING="var=value&arg=value" ; \
php -e -r 'parse_str($_SERVER["QUERY_STRING"], $_GET); include "index.php";'

请注意,您也可以对$_POST$_COOKIE做同样的事情.

Note that you can do the same with $_POST and $_COOKIE as well.

这篇关于PHP在Linux命令提示符中传递$ _GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆