I need to automate daily calls to a single PHP script but passing a different parameter each time (around 30). Is there a neat way to handle this, other than creating an individual cron for each call to the script? THanks for any pointers.
- 3What determines which parameter is passed to the script, just the time?AJ.– AJ.2011-05-23 14:48:45 +00:00Commented May 23, 2011 at 14:48
- There's an array of around 30 numbers which all need to be passed individually to the script - the params are a number of days, each being used as filters in a queryspinozf– spinozf2011-05-23 14:51:32 +00:00Commented May 23, 2011 at 14:51
3 Answers
You can run the script using different command line parameters. Check getopt function or $argv/$argc variables.
Comments
The $argv array will hold multiple parameters:
http://php.net/manual/en/reserved.variables.argv.php
<?php var_dump( $argv ); ?> Example usage:
$ php cli.php param1 param2 param3 Output:
array(4) { [0]=> string(7) "cli.php" [1]=> string(6) "param1" [2]=> string(6) "param2" [3]=> string(6) "param3" } Comments
You could create a parameters.txt file with all 30 or so parameters separated by line breaks. Then create a separate counter.txt file with a 0 in it.
Read the counter.txt file. Read the parameters file, split it by line breaks, and then access the index location indicated by the counter file to get your parameter.
(your file logic here)
Increment the counter, write the new counter to counter.txt (overwrite it), and continue the process, resetting the counter once you have reached your upper limit.