views:

60

answers:

4

Hi i have a php script that accepts a POST request as a listener to a web service then process all the data to two final arrays, I'm looking for a way to initiate a second script that GET's those serialized arrays and do some more processing. include() will not be good for me since i actually want to "free" or "end" the first script after passing the data

your help is much appreciated as always :)

EDIT - OK so looks like queue might be the solution! i never did anything like this before any examples or reference?

+2  A: 

Does it need to happen immediately? Otherwise you could set up a cronjob that does that every X minutes. You'll have to make some kind of queue in which your first script sticks "requests" to the second script. The cronjob then processes the requests in the queue.

Bart van Heukelom
+1 because it actually answers the question (which seems to be about doing processing after ending the http request)
timdev
Bart - thanks for your reply could you give an example? or point me to a reference? i never did something like this before
Yaniv
A: 

I'd do it all in one request. It cuts down on latency and makes the whole operation more efficient.

Remember you can have a long running request, but still service other requests. Apache will just spawn another php process to handle the other request from the webservice even though the first has not completed. As long as the script doesn't lock a shared resource (database file etc) this will work just fine.

That said, you should use cURL to call the second script. then post the unserialized array. cUrl will handle the rest.

Byron Whitlock
Not always a good idea. If the secondary process is slow, and you don't need the results before the end of the http request, you might as well queue a job and have some back-end process that chews through the queue. Keeping http connections open, especially under mod_php (which means apache is running in mpm_prefork) can chew up available memory pretty quickly. A well-tuned mod_php apache server will have a finite number of possible child processes. If it's well tuned, you'll run out of listeners and the server will stop accepting requests. If not, you'll run out of memory and deadlock.
timdev
+1  A: 

You should get into the habit of writing php scripts that are just a collection of functions (no auto-ran scripts, per se). This way you can include a script file at the top of the script your talking about and then call the function that does what you want.

For instance:

<?php
include('common_functions.php');
$array_1 = whatever_you_do_with_post_values();
$array_2 = other_thing_you_do_with_post_values();
// this function is located in 'common_functions.php'
do_stuff_with_arrays($array_1,$array_2); 
?>

In Fact:

Just to be consistent with what I'm saying:

<?php
include('common_functions.php');
do_your_stuff();
function do_your_stuff() {
    $array_1 = whatever_you_do_with_post_values();
    $array_2 = other_thing_you_do_with_post_values();
    // this function is located in 'common_functions.php'
    do_stuff_with_arrays($array_1,$array_2); 
}
?>

Obviously you should use better function & variable names, haha.

KyleFarris
Yes. auto running scripts are a nightmare to maintain and susceptible to failure.
Byron Whitlock
@kyle: php is a single pass parser. notice anything wrong with your code ;)
Byron Whitlock
@Byron: Nope. If you're referring to the fact that I'm calling the function before it's declared, I've always done that and it's never been a problem. If that's *not* what you're alluding to, please enlighten me 'cause I'll go crazy knowing there is something wrong and I can't see it o_0.
KyleFarris
Even better, use classes and autoloading. Then you don't have to manage the includes.
Bart van Heukelom
@Kyle maybe newer versions of php have fixed this (I learned some conventions in php3). or maybe i am mixing up javascript, php, tcl and perl. In normal week I'll use them all :D
Byron Whitlock
@Byron: Totally understandable. I know for a fact in JavaScript, Perl, and PHP, it doesn't matter. I believe in languages like VB, though, it does matter. Can't say anything about TCL.
KyleFarris
A: 

To immediately answer your question, if you pass a GET variable through file_get_contents, the server will process that value as if an outside source had requested it:

file_get_contents( "http://www.example.com/runSomething.php?foo=132" );

In the above example, runSomething.php has now received the information $_GET[ "foo" ] = 123. This is a bit awkward, though. It also will not technically free any resources, you'll now have one script waiting for a response, and another script taking up the same memory which would normally have been used by your original script. That said, you may be spawning another instance of PHP, which could help.

You are much better off if you can encapsulate the functionality into two objects or two functions and then call them in succession. If this is done through oop, you'll be able to call unset to remove the first object from memory.

If this is something which can happen asynchronously, I agree with Bart. cron is very powerful, and, if you're using Linux, you'll even be able to call PHP from the command line so you won't have to learn much beyond what you already know.

Christopher W. Allen-Poole