37

I need to execute a php file with parameters through shell.

here is how I would run the php file:

php -q htdocs/file.php

I need to have the parameter 'show' be passed through and

php -q htdocs/file.php?show=show_name

doesn't work

If someone could spell out to me what command to execute to get the php file to execute with set parameters, it would be much appreciated. If not, try to lead me the right direction.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Patrick Lorio
  • 5,520
  • 11
  • 45
  • 74

5 Answers5

55

test.php:

<?php
print_r($argv);
?>

Shell:

$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)
Neil
  • 14,063
  • 3
  • 30
  • 51
schneck
  • 10,556
  • 11
  • 49
  • 74
  • 2
    Ok, so I cannot receive the parameters via $_GET in php but through $argv, thanks. It took me a while to figure that out. – Patrick Lorio Jul 20 '11 at 15:39
7

If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this

wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1

where:

  • « -O - » — (Letter "O", not zero!) redirect "downloaded html" to stdout
  • « >/dev/null 2>&1 » — redirect stdout & stderr output to nowhere
  • « -q » — quiet wget run
  • « -t 1 » — just 1 try to connect (not like default 20)

In PHP's "exec" it'll be smth like this:

function exec_local_url($url) {
  exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
    . addslashes($url) . '" >/dev/null 2>&1'
  );
}

// ...

exec_local_url("file.php?show=show_name");
exec_local_url("myframework/seo-readable/show/show_name");

So, you don't need to change your scripts to handle argc/argv, and may use $_GET as usually do.

If you want jobs runned in background - see for ex. Unix/Windows, Setup background process? from php code

I use approach with wget in my cron jobs; hope it helps.

Community
  • 1
  • 1
FlameStorm
  • 944
  • 15
  • 20
  • Is there any difference between using `wget` and `php` commands when running jobs with `exec` regarding security or performace? – Gixty Jul 06 '15 at 23:58
  • I sure security is the same. Performance - of course `wget` will be more slow but much handy as of said upper. With wget the request goes all hidden stages or request handling like apache/nginx/lighttpd caches, rewrites and so on. When in direct call of `php` you will blame so complex workaround,- i.e. it's appliable not in all cases. Anyway, I think you just must take your current case, your brain, benchmarking tool and just shuffle it a good! ;) – FlameStorm Apr 21 '16 at 21:41
3

You need to read command line parameters from $argc and $argv.

Using a question mark is something you do in a URL and has nothing to do with executing PHP from a command line.

See also: http://www.sitepoint.com/php-command-line-1/

Brad
  • 159,648
  • 54
  • 349
  • 530
3

In addition to the other answers (Which are quite correct), you can also pass arguments as environment parameters, like this:

FOO=42 BAR=quux php test.php

They will then be available in the superglobal $_ENV.

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • when I enter that command I get an error for the "FOO". I'm using xampp's shell command on Windows, does that change anything? – Patrick Lorio Jul 20 '11 at 15:21
  • It probably won't work on Windows, but it'll work fine on most unix type shells. – troelskn Jul 20 '11 at 17:36
  • I got it to work, I was expecting it to be received by PHP's $_GET[] function. It took me a while to realize that $arvg held all the parameters. Thanks. – Patrick Lorio Jul 21 '11 at 16:03
0

If you are using it from a PHP file then you can use popen() and do something like this:

$part = $show_name; //or whatever you want with spaces

$handle = popen("php -q nah.php -p=". escapeshellarg($part) . " 2>&1", "r");

This uses the escapeshellarg() function in order to wrap the $part variable in quotes (and escape any quotes inside it), so that it can be used as a shell argument safely.

Diemuzi
  • 3,507
  • 7
  • 36
  • 61
kritya
  • 3,312
  • 7
  • 43
  • 60