Connecting Tech Pros Worldwide Help | Site Map

Pass equivalent $_POST[] array via command line

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,078
#1: Jun 3 '08
The following code is part of a script actioned by a web form
[PHP]if(isset($_POST['report']))#the form has been submitted
{
if($pls = $_POST['pls']) #hidden variable in pricesCostForm
{
$levels = array();
for($c=0;$c<$pls;$c++)
{
if(isset($_POST['level'.$c]))
$levels[] = trim($_POST['level'.$c]);
} [/PHP] What do I add to the following command line
Expand|Select|Wrap|Line Numbers
  1. C:\apache\xampp\php\php-win.exe  "C:\apache\xampp\htdocs\programs\Reports\Prices\pricesReportEntry.php"
so that $_POST['report'] is set
$_POST['pls'] equals 45
and $_POST['level'.42] = 'TEST'
so that the above code will create the array
Expand|Select|Wrap|Line Numbers
  1. $levels[0=>'TEST'] 
This so the script can run via a scheduled task.
Thanks
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,746
#2: Jun 3 '08

re: Pass equivalent $_POST[] array via command line


You should be able to fetch command line arguments via the $argv array. Like, if you type into the cmd window:
Expand|Select|Wrap|Line Numbers
  1. C:\apache\xampp\php\php-win.exe  "C:\Path\To\File\myphpfile.php" first second third
  2.  
And you had this in the myphpfile.php
Expand|Select|Wrap|Line Numbers
  1. <?
  2. print_r($argv);
  3. ?>
  4.  
It should output something like:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => myphpfile.php
  4.     [1] => first
  5.     [2] => second
  6.     [3] => third
  7. )
  8.  
  9.  
Don't have a Windows machine to test this on, but it does work on Linux.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#3: Jun 3 '08

re: Pass equivalent $_POST[] array via command line


You know, I've never attempted PHP from the command line. Like OOP, it's always intimidated me.

Doesn't look to hard, actually. So I might give it a shot!
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#4: Jun 3 '08

re: Pass equivalent $_POST[] array via command line


Quote:

Originally Posted by markusn00b

So I might give it a shit!

I hope you don't.

A shot will be better.

Once you go OOP you never go back.

Command line is good for cron jobs. (scheduled tasks in Windows)

Maybe you need to clean up temp directories, clean up session table. FTP file somewhere, etc.

You could do it with PHP!
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#5: Jun 3 '08

re: Pass equivalent $_POST[] array via command line


Quote:

Originally Posted by dlite922

I hope you don't.

A shot will be better.

Once you go OOP you never go back.

Command line is good for cron jobs. (scheduled tasks in Windows)

Maybe you need to clean up temp directories, clean up session table. FTP file somewhere, etc.

You could do it with PHP!

Oh dear.

What a bad typo!
I can't even edit it!

Jeeeez.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#6: Jun 4 '08

re: Pass equivalent $_POST[] array via command line


SuperMod to the rescue!

*sound of engine sputtering*

Oops. Forgot; my cape hasn't been reactivated yet. Well, it'll be a funny inside joke that we can't ever tell anyone else about ever.

Anyway. Here's everything you ever wanted to know and more about running PHP from the command line.

You can't simulate $_POST variables via CLI. But you could do something like this, if the code had to be runnable from both the command line and the web:

Expand|Select|Wrap|Line Numbers
  1. define('IS_CLI', php_sapi_name() == 'cli');
  2.  
  3. if( IS_CLI )
  4. {
  5.   $input =& $argv;
  6. }
  7. else
  8. {
  9.   $input =& $_POST;
  10. }
  11.  
  12. if( isset($input['report']) ) /** and so on */
  13.  
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,078
#7: Jun 4 '08

re: Pass equivalent $_POST[] array via command line


Thanks Atli. The argv array, what else.
The same as C hm.
I should have realised.
So I can tag the variables to the command line.
And then use pb_mods suggestion to edit my php
Thanks a lot guys
Reply