Page 1 of 1

PHP Parameters - Command Line

Posted: Mon Apr 19, 2010 7:55 am
by timWebUK
I did try searching Google, but I really was not sure exactly what to find so the results I got back were very sparce so I was hoping you guys could help as it is probably a simple issue.

I'm writing a PHP function that takes a file path as a parameter, how can I run this PHP function from the command line and pass the parameter that way?

Cheers.

Re: PHP Parameters - Command Line

Posted: Mon Apr 19, 2010 8:17 am
by requinix
You can't run just functions from the command line. Only entire files.

When run from the command line, $argv and $argc correspond to the C/C++ equivalents: $argv[0] is the script being run, [1] is the first argument, [2] the second, and so on. $argc is the number of arguments - aka count($argv).

Re: PHP Parameters - Command Line

Posted: Mon Apr 19, 2010 8:22 am
by timWebUK
Ah ok, that makes sense! Cheers, thanks for the fast response.

Re: PHP Parameters - Command Line

Posted: Tue Apr 20, 2010 8:40 am
by katierosy
Most important is space in query string as below.

in a php file say test.php these lines.

$query_string = ' id=' . '1000' .' IMG_SRC=' . 'URL';

$file = "x.php";

exec("nohup /usr/bin/php -f " . $file . $query_string . " > /tmp/mycronlog.txt &");

and in another php file namely x.php

for($i=1;$i<count($argv);$i++){
$request = split('=',$argv[$i]);
$_GET[$request[0]] = $request[1];
}

you will get $_GET['IMG_SRC'] and $_GET['id'] like this