Organizational Research By

Surprising Reserch Topic

php passing get in linux command prompt


php passing get in linux command prompt  using -'php,linux'

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!
    

asked Sep 9, 2015 by rajesh
0 votes
11 views



Related Hot Questions

9 Answers

0 votes

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 refers to the HTTP GET method parameters, which are unavailable in command line, since they require a web server to populate.

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

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";'

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

answered Sep 9, 2015 by rajesh
0 votes

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
answered Sep 9, 2015 by rajesh
0 votes

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

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

You set foo to 123.


%cat test1.php
You set foo to .
answered Sep 9, 2015 by rajesh
0 votes
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.

answered Sep 9, 2015 by rajesh
0 votes

I just pass them like this:

php5 script.php param1=blabla param2=yadayada

works just fine, the $_GET array is:

array(3) {
  ["script_php"]=>
  string(0) ""
  ["param1"]=>
  string(6) "blabla"
  ["param2"]=>
  string(8) "yadayada"
}
answered Sep 9, 2015 by rajesh
0 votes

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

php-cgi -f /unixPathToWeb/www/index.php a=1 b=2 c=3

Another option is 'wget':

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

OR if you wish to run it from a bash script try:

wget -q -O - "http://localhost/index.php?a=1&b=2&c=3"
answered Sep 9, 2015 by rajesh
0 votes

Sometimes you don't have the option of editing the php file to set $_GET to the parameters passed in, and sometimes you can't or don't want to install php-cgi.

I found this to be the best solution for that case:

php -r '$_GET["key"]="value"; require_once("script.php"); 

This avoids altering your php file and lets you use the plain php command. If you have php-cgi installed, by all means use that, but this is the next best thing. Thought this options was worthy of mention

the -r means run the php code in the string following. you set the $_GET value manually there, and then reference the file you want to run.

Its worth noting you should run this in the right folder, often but not always the folder the php file is in. Requires statements will use the location of your command to resolve relative urls, NOT the location of the file

answered Sep 9, 2015 by rajesh
0 votes

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

php -a

Then type:

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

answered Sep 9, 2015 by rajesh
0 votes

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'

answered Sep 9, 2015 by rajesh

...