PHP passing $_GET in linux command prompt

2019-01-02 22:10发布

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?

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.

Thank you!

标签: php linux
13条回答
再贱就再见
2楼-- · 2019-01-02 22:28

Try using WGET:

WGET 'http://localhost/index.php?a=1&b=2&c=3'
查看更多
爷、活的狠高调
3楼-- · 2019-01-02 22:29

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];'\'

then simply use:

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

查看更多
劫难
4楼-- · 2019-01-02 22:34
php file_name.php var1 var2 varN

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

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

the variables you launch the script with will be accessible from the $argv array 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.

查看更多
狗以群分
5楼-- · 2019-01-02 22:36

If you have the possibility to edit the PHP script, you can artificially populate $_GET array using the following code at the beginning of the script and then call the script with the syntax: php -f script.php name1=value1 name2=value2

// When invoking the script via CLI like "php -f script.php name1=value1 name2=value2", this code will populate $_GET variables called "name1" and "name2", so a script designed to be called by a web server will work even when called by CLI
if (php_sapi_name() == "cli") {
    for ($c = 1; $c < $argc; $c++) {
        $param = explode("=", $argv[$c], 2);
        $_GET[$param[0]] = $param[1]; // $_GET['name1'] = 'value1'
    }
}
查看更多
来,给爷笑一个
6楼-- · 2019-01-02 22:39

From this answer on ServerFault:

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

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

Which puts this in $_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:

REQUEST_URI='/index.php' SCRIPT_NAME='/index.php' php-cgi -f index.php left=1058 right=1067 class=A language=English
查看更多
贼婆χ
7楼-- · 2019-01-02 22:44

-- Option 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 -f /my/script/file.php a=1 b=2 c=3

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

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

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

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

$a = $_GET["a"];
$b = $_GET["b"];
$c = $_GET["c"];
查看更多
登录 后发表回答