3

I have a PHP file that invokes another PHP file via curl. I am trying to have the second file send a response back to the first to let it know that it started. The problem is the first can't wait for the first to finish execution because that can take a minute or more, I need it to send a response immediately then go about it's regular business. I tried using an echo at the top of the second file, but the first doesn't get that as a response. How do I send back a response without finishing execution?

file1.php

<?php
$url = 'file2.php';
$params = array('data'=>$data,'moredata'=>$moredata);

$options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "Mozilla", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    CURLOPT_TIMEOUT        => 10,        // don't wait too long
    CURLOPT_POST           => true,     // Use Method POST (not GET)
    CURLOPT_POSTFIELDS     => http_build_query($params)
);
$ch = curl_init($url);

curl_setopt_array( $ch, $options );
$response = curl_exec($ch); // See that the page started.
curl_close($ch);
echo  'Response: ' . $response;
?>

file2.php

<?php
/* This is the top of the file. */
echo 'I started.';
.
.
.
// Other CODE
.
.
.
?>

When I run file1.php it results in: 'Response: ' but I expect it to be 'Response: I started.' I know that file2.php gets started because 'Other CODE' get executed, but The echo doesn't get sent back to file1.php, why?

6
  • I think you should clarify your question, and add some code. Commented Aug 26, 2011 at 1:02
  • If the second script can take some time to be executed, why the 10 second execution time limit? Commented Aug 26, 2011 at 1:42
  • Because I can not wait for the second script to finish. I need the second script to immediately send a response back to file1 and then continue on with its code Commented Aug 26, 2011 at 1:49
  • If you just need to know that the second file was called, curl already does that for you, check the options available. What you're doing doesn't make much sense. Commented Aug 26, 2011 at 2:34
  • I need the "curl'ed" script to send me back a process id Commented Aug 26, 2011 at 4:09

2 Answers 2

1

This could be just what you're looking for. Forking in PHP:

http://framework.zend.com/manual/en/zendx.console.process.unix.overview.html

A process divides in two. One is father of the other. The father can tell the client he just begun and the child can do the job. When the child finishes, he's able to report the father which can also report to the client.

Keep in mind there are many requirements for this to run:

  • Linux
  • CLI or CGI interface
  • shmop, pcntl and posix extensions (require recompiling)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I understand forking. But I can not use it here because I need file1.php to finish quickly so it can get back to our user, where as file2.php can take minutes to finish.
1

The answer ended up being that CURL does not behave like a browser:

PHP Curl output buffer not receiving response

I ended up running my 2nd file first and my 1st file second. The 2nd file waited for a 'finished' file write that the 1st file did once it, obviously, finished.

At this point, it seems like the database would be a better place to store messages for files to be able to pass between each other, but a file would also work for a quick and dirty job.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.