views:

42

answers:

2

Hi,

For a project I need to schedule several PHP scripts. Some of the scripts are a bit complicated (ie. send mail, do database stuff, use several external webservices, use external files and classes etc). The problem is that these scripts also need to be called from the browser, and they do use some of the variables in the $_SERVER array (including HTTP_HOST).
Is there a way to make these kind of scripts work from the commandline, or do I have to rewrite them to be 'commandline-compatible'? Or is it better to use parameters from the commandline that specify the variables that are not available?

We have a server running Windows Server 2008.

+2  A: 

You should abstract the part of script that is common to browser and command line environment. Then you can include it into two scripts, one of which will use $_SERVER, another one command line arguments variables to run the common part.

Mchl
This seems like a good idea, thanks.
Lex
+1 for a good idea.
Brad F Jacobs
The downside for me is that the pieces that use $_SERVER are in multiple included files.
Lex
This turned out to work pretty well, sorry for the delayed accepting, I kind of forgot about this question...
Lex
A: 

Best practices in scheduling PHP scripts?

1) don't. It's a slippery slope - you end up spending far too much time trying to organise the scripts into available time slots, you end up with a system which changes state depending on which jobs have been run and haven't. Really you want all processing triggered directly in response to user requests. (that doesn't mean that they have to work synchronously and in the same execution thread as the user request though).

2) don't do it on MSWindows - Microsoft operating systems are OK for providing GUIs but do not have the range of tools available for job management and automation available on other Operating Systems

I've seen a lot of cases where people shove stuff into scheduled batches simply because the performance of their code sucks. In a lot of cases a better solution would be to fix the performance problem.

Having said that, since you're already in a hole, yes, you could duplicate all your code into command line and web versions - or you could download wget or cURL and schedule these programs to invoke your URLs passing the required values via GET or POST

symcbean