141

Say we usually it access via

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

How do we execute the same on a 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? I doubt that'll work.

13 Answers 13

274

From this answer on Server Fault:

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
Sign up to request clarification or add additional context in comments.

5 Comments

+1 and +1: This answer should have been the accepted answer. Much less hassle! No need to change the php source.
Excellent, this could be easily wrapped in any OS-dependent script.
+1, awesome solution, I'm currently backporting all my CLI scripts to support this now ;)
The only downside I've seen so far is that the terminal output is HTML with <br /> rather than just \n. This applies to all notices and errors as well, e.g. <b>Notice</b>: Undefined index: testing in <b> ... </b><br />
+1 Awesome, This works in Normal Script $_GET Value For Ex as : php-cgi -f /var/www/vhosts/example.com/public_html/index.php action=DoSomething Works Good..
132

Typically, for passing arguments to a command line script, you will use either the 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 on the 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.

5 Comments

It's worth noting that, on our Centos 6 machine running PHP 5.3, calling php [script name] "a=1&b=2&c=3" will not populate the $_SERVER['QUERY_STRING'], but you can easily affect the same thing by referencing $_SERVER['argv'][1].
Try this answer to populate the query string from the command line without modifying the PHP script.
This is not the best answer. See this other answer on this page: stackoverflow.com/a/11965479/543738
Helpful answer, though if like me you're wondering what those colons are after argument names in getopt(), this apparently tells PHP you want the argument f, not the option f (which would be specified minus the colon). So it's just syntax.
I really like the last answer, and I used it yesterday. It can be turned into a script, too.
26

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

It's 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.

Comments

17

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
<html>
You set foo to 123.
</html>

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

Comments

15

Use:

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

2 Comments

the easiest and simpler solution
That's the solution! GET is simply not made for command line action
10

Try using WGET:

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

4 Comments

You'd want to enclose the URL in single quotes, as the ? and & in the query portion are shell metacharacters (single char wildcard + "run this command in the background").
This will work, but requires a running web server, and makes the whole process a tad bit more inefficient than is required.
It does, but typically $_GET implies a running webserver.
WGET? Don't you mean wget?
6

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

Comments

4

If you have the possibility to edit the PHP script, you can artificially populate the $_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'
    }
}

Comments

3

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

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.

Comments

1

At the command line, paste the following:

export QUERY_STRING="param1=abc&param2=xyz" ;  
POST_STRING="name=John&lastname=Doe" ; php -e -r 
'parse_str($_SERVER["QUERY_STRING"], $_GET); parse_str($_SERVER["POST_STRING"], 
$_POST);  include "index.php";'

Comments

0

I just pass them like this:

php5 script.php param1=blabla param2=yadayada

It works just fine. The $_GET array is:

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

3 Comments

just found out this works on my hosting server, but not on my local server, freaky.
Maybe a difference in the installed version? Which version did this work for you on, and which did it fail?
php 5.2 on hosting, works, 5.3 locally doesn't... doesn't matter I did it the $argv way just in case the $_GET is empty
-1

Use:

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'

Comments

-3

Or just (if you have Lynx):

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

1 Comment

This does not run PHP from the command line. This runs a browser which invokes the web server through the command line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.