Sam
Sam

Reputation: 125

Spawning a PHP script from inside another. Non-blockning

I have a PHP script that I need to execute from inside another PHP webpage. However for the second one to run properly the first needs to have fully completed. Essentially I need the first page to spawn a new process/thread for the second script which will wait 1 second before starting.

Doing an include causes blocking which prevents it from working and I can't get it to start using exec

Edit:

Should have clarified. These pages have no output and are not interfaced with through a web interface. All pages are called by POST requests from another server.


Edit 2:

Solution: make server requesting the page send a request directly to the second page 1 second after the first returns.

Upvotes: 6

Views: 555

Answers (3)

Anthony Hildoer
Anthony Hildoer

Reputation: 545

proc_open is the correct choice, as @ChristopherMorrissey pointed out. I want to elaborate a little here, as there are some caveats to using proc_open that aren't entirely obviously.

In the first code example @ http://php.net/manual/en/function.proc-open.php, it shows the overall usage and I will reference that.

The first caveat is with the pipes. The pipes are file streams in PHP that link to STDIN, STDOUT and STDERR of the child process. In the example, pipe index 0 represents a file stream from the parent PHP processes perspective. If the parent process writes to this stream, it will appear as STDIN input to the child process.

On POSIX compliant OSes, STDIN to a process needs to close before the process can terminate. Its very important to call fclose on the pipe from the parent, or your child process will be stuck. That is done with this line in the example:

fclose($pipes[0]);

The other caveat is on checking the exit code of the child process. Checking the exit code is the best way to determine if the child process has exited correctly, or if it erred out. At the very least, you will need to just know when the child process has completed. Checking this and the exit code are both done with http://www.php.net/manual/en/function.proc-get-status.php

If you want to ensure the process exits correctly, you will need to look at the exitcode field returned in the array from proc_get_status. Keep in mind this exit code will only return a valid value once. All other times it will return -1. So, the one time it returns > -1, this is your actual exit code. So, the first time running == false, check exitcode.

I hope this helps.

Upvotes: 1

pattyd
pattyd

Reputation: 6150

One option would be to redirect the page after the first script has finished.

You could do it this way:

//first script here
sleep(1); //wait one second
echo "<meta http-equiv=\"refresh\" content=\"0;URL='yoursecondscript.php'\" />"; //redirect

or even:

//first script here
//redirect after one sec 
echo "<meta http-equiv=\"refresh\" content=\"1;URL='http://thetudors.example.com/'\" />";

Upvotes: 1

James G.
James G.

Reputation: 2904

You might be able to write a header going to the second page when you want it, then putting a header back to the original page at the end of the second page. Though there are many reasons this fix wouldn't work, like the PHP code being required to be in the HTML.

Headers reference So something like: header(Location: seconddocument.php)

Or maybe you could put the PHP to execute in a function, and then call it from the original PHP document. I can't be exactly sure of your requirements here, but those would be my two best answers.

Upvotes: 0

Related Questions