PHP 在 linux 命令提示符下传递 $_GET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4186392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:52:03  来源:igfitidea点击:

PHP passing $_GET in linux command prompt

phplinux

提问by Patrick

Say we usually access via

假设我们通常通过访问

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

How do we execute the same in linux command prompt?

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

php -e index.php

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

但是如何传递 $_GET 变量呢?也许像 php -e index.php --a 1 --b 2 --c 3?怀疑这会奏效。

Thank you!

谢谢!

采纳答案by netcoder

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

通常,要将参数传递给命令行脚本,您将使用argv全局变量或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 refers to the HTTP GET method parameters, which are unavailable in command line, since they require a web server to populate.

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

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

如果你真的想填充 $_GET ,你可以这样做:

// 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
     )
*/

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

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

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

Note that you can do the same with $_POSTand $_COOKIEas well.

请注意,您也可以使用$_POST和执行相同操作$_COOKIE

回答by Giuseppe Accaputo

Try using WGET:

尝试使用WGET

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

回答by T. Brian Jones

php file_name.php var1 var2 varN

Then set your $_GETvariables on your first line in PHP, although this is not the desired way of setting a $_GETvariable and you may experience problems depending on what you do later with that variable.

然后$_GET在 PHP 的第一行设置变量,尽管这不是设置$_GET变量的理想方式,并且您可能会遇到问题,具体取决于您稍后对该变量的操作。

if (isset($argv[1])) {
   $_GET['variable_name'] = $argv[1];
}

the variables you launch the script with will be accessible from the $argvarray in your PHP app. the first entry will the name of the script they came from, so you may want to do an array_shift($argv)to drop that first entry if you want to process a bunch of variables. Or just load into a local variable.

可以从$argvPHP 应用程序中的数组访问用于启动脚本的变量。第一个条目将是它们来自的脚本的名称,因此array_shift($argv)如果您想处理一堆变量,您可能需要删除第一个条目。或者只是加载到局部变量中。

回答by Asaf

I just pass them like this:

我只是像这样传递它们:

php5 script.php param1=blabla param2=yadayada

works just fine, the $_GET array is:

工作得很好, $_GET 数组是:

array(3) {
  ["script_php"]=>
  string(0) ""
  ["param1"]=>
  string(6) "blabla"
  ["param2"]=>
  string(8) "yadayada"
}

回答by qris

From this answer on ServerFault:

从这个关于 ServerFault 的答案

Use the php-cgibinary instead of just php, and pass the arguments on the command line, like this:

使用php-cgi二进制文件而不仅仅是php,并在命令行上传递参数,如下所示:

php-cgi -f index.php left=1058 right=1067 class=A language=English

Which puts this in $_GET:

这把它放在$_GET

Array
(
    [left] => 1058
    [right] => 1067
    [class] => A
    [language] => English
)

You can also set environment variables that would be set by the web server, like this:

您还可以设置将由 Web 服务器设置的环境变量,如下所示:

REQUEST_URI='/index.php' SCRIPT_NAME='/index.php' php-cgi -f index.php left=1058 right=1067 class=A language=English

回答by Tim Booth

I don't have a php-cgi binary on Ubuntu, so I did this:

我在 Ubuntu 上没有 php-cgi 二进制文件,所以我这样做了:

% alias php-cgi="php -r '"'parse_str(implode("&", array_slice($argv, 2)), $_GET); include($argv[1]);'"' --"
% php-cgi test1.php foo=123
<html>
You set foo to 123.
</html>

%cat test1.php
<html>You set foo to <?php print $_GET['foo']?>.</html>

回答by makman99

If you need to pass $_GET, $_REQUEST, $_POST, or anything else you can also use PHP interactive mode:

如果您需要传递$_GET$_REQUEST$_POST或其他任何内容,您也可以使用 PHP 交互模式:

php -a

Then type:

然后输入:

<?php
$_GET['a']=1;
$_POST['b']=2;
include("/somefolder/some_file_path.php");

This will manually set any variables you want and then run your php file with those variables set.

这将手动设置您想要的任何变量,然后使用这些变量设置运行您的 php 文件。

回答by quardas

or just (if you have LYNX):

或者只是(如果你有 LYNX):

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

回答by Meow

php -r 'parse_str($argv[2],$_GET);include $argv[1];' index.php 'a=1&b=2'

php -r 'parse_str($argv[2],$_GET);include $argv[1];' index.php 'a=1&b=2'

You could make the first part as an alias:

您可以将第一部分作为别名:

alias php-get='php -r '\''parse_str($argv[2],$_GET);include $argv[1];'\'

alias php-get='php -r '\''parse_str($argv[2],$_GET);include $argv[1];'\'

then simply use:

然后简单地使用:

php-get some_script.php 'a=1&b=2&c=3'

php-get some_script.php 'a=1&b=2&c=3'

回答by Bastion

-- Option 1: php-cgi --

-- 选项 1: php-cgi --

Use 'php-cgi' in place of 'php' to run your script. This is the simplest way as you won't need to specially modify your php code to work with it:

使用 'php-cgi' 代替 'php' 来运行您的脚本。这是最简单的方法,因为您无需专门修改 php 代码即可使用它:

php-cgi -f /my/script/file.php a=1 b=2 c=3

-- Option 2: if you have a web server --

-- 选项 2:如果您有网络服务器 --

If the php file is on a web server you can use 'wget' on the command line:

如果 php 文件在 Web 服务器上,您可以在命令行上使用“wget”:

wget 'http://localhost/my/script/file.php?a=1&b=2&c=3'

OR:

或者:

wget -q -O - "http://localhost/my/script/file.php?a=1&b=2&c=3"

-- Accessing the variables in php --

-- 在 php 中访问变量 --

In both option 1 & 2 you access these parameters like this:

在选项 1 和 2 中,您可以像这样访问这些参数:

$a = $_GET["a"];
$b = $_GET["b"];
$c = $_GET["c"];