I\'m trying to write down a script to fetch some online data; script should be invoked either by a cron job or php cli and with standard GET HTTP re
The manual didn't state this very well, but, if you want $_SERVER['argc']
, $_SERVER['argv']
, $argc
, $argv
to be registered when you are not running in CLI
mode, then the php.ini
value register_argc_argv needs to be enabled in php.ini (off by default [for performance reasons]).
You could do the following to get argv
, or query string args depending on how the script is running:
if (php_sapi_name() == 'cli') {
$args = $_SERVER['argv'];
} else {
parse_str($_SERVER['QUERY_STRING'], $args);
}
Here are some details from php.ini
:
; This directive determines whether PHP registers $argv & $argc each time it
; runs. $argv contains an array of all the arguments passed to PHP when a script
; is invoked. $argc contains an integer representing the number of arguments
; that were passed when the script was invoked. These arrays are extremely
; useful when running scripts from the command line. When this directive is
; enabled, registering these variables consumes CPU cycles and memory each time
; a script is executed. For performance reasons, this feature should be disabled
; on production servers.
; Note: This directive is hardcoded to On for the CLI SAPI
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/register-argc-argv
See also http://www.php.net/manual/en/reserved.variables.argv.php and parse_str().
You will have to use $_GET
or $_SERVER['argv']
depending on how your script is called. Neither one is used for both.
For example:
if(!empty($_SERVER['argv'][0]) {
$a = $_SERVER['argv'][1];
$b = $_SERVER['argv'][2];
} else {
$a = $_GET['a'];
$b = $_GET['b'];
}