Jump to content


Photo

PHP Cron and $_GET values


  • Please log in to reply
6 replies to this topic

#1 jtravis

jtravis
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 April 2006 - 03:55 AM

Hey all! I was wondering if anyone would know how to run a php script from a shell script or cron job and pass $_GET data to it?

I have a script for my cart (that was a module that I didn't code) that generates a file to be uploaded to Froogle. So basically, I'm wanting something like this in my script.

php -q /path_to_file/file.php?data1=value1&data2=value2

Is this possible?


Thanks!

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 April 2006 - 04:34 AM

The $_GET & $_POST superglobal arrays are only filled when your script is invoked via a web server. When invoked via the command line, parameters are passed in the $argv array, just like C.

In you case, you should invoke it in one of these ways:
  • php -q /path_to_file/file.php data1=value1&data2=value2
    And then use the [a href=\"http://www.php.net/parse_str\" target=\"_blank\"]parse_str[/a]() function to get the values:
    <?php parse_str($argv[1]) ?>
  • php -q /path_to_file/file.php value1 value2
    and process it like
    <?php for ($i=1;$i <= $argc; $i++) $data[$i] = $argv[$i] ?>
The first index of $argv (i.e. $argv[0]) is the script name.

Ken

#3 jtravis

jtravis
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 April 2006 - 01:21 PM

Just making sure I understand.

I would have to alter the original script to look for the $argv value instead of the $_GET values or both.

How is using parse_str() different from $_SERVER['argv'] (I was RTFM ;))?

Just curious. Is the $argv an associative array as well? Meaning instead of using a numerical index, can I reference it by name as in $_GET?

Thanks for your reply! Much appreciated!

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 10 April 2006 - 01:47 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Is the $argv an associative array as well?[/quote]
No.

#5 jtravis

jtravis
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 April 2006 - 04:22 PM

I suppose my example was poor, or I'm just not grasping the concept.

Would this work or must I use the $argv?

//Parse the text to get the values.
parse_str('download=tempfile&dltype=froogle');
//Replaced $_GET with $_SERVER in attempt to run from cli
$ep_dltype = (isset($_SERVER['dltype'])) ? $_SERVER['dltype'] : $ep_dltype;




#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 10 April 2006 - 04:51 PM

If this is all this cron job is going to do and these values aren't going to be in any way dynamic, why not just hard code the values into the script?

#7 jtravis

jtravis
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 10 April 2006 - 04:56 PM

[!--quoteo(post=363358:date=Apr 10 2006, 12:51 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Apr 10 2006, 12:51 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If this is all this cron job is going to do and these values aren't going to be in any way dynamic, why not just hard code the values into the script?
[/quote]


Good point! I guess I was thinking so outside the box that I didn't think to look inside of it. I suppose part of it was I wasn't for certain how the script ran as it's a third-part mod and was wanting to see if this way would be easier. But to be honest. I never even thought of that. LMAO!

Thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users