PHP Command Line/Cron Arguments

Run the PHP file from the command line or by cron with an argument:

php -q /home/user/file.php --foo=bar

These can be accessed in the PHP script with $argv:

var_dump($argv);

Turn the args into $_GET vars so that either can be used:

foreach ($argv as $arg) {
$e=explode("=",$arg);
if(count($e)==2)
$_GET[ltrim($e[0],'-')]=$e[1];
else 
$_GET[$e[0]]=0;
}

var_dump($_GET);

Leave a Reply

Your email address will not be published.