1

I have a deal where file1.php curl runs file2.php. file2.php is a long running file, but it sends(or is supposed to send) a response back to file1.php then carry on with it's code. I am using output buffer to try sending this data, but the problem is if I 'return;' right after the flush; file1.php receives the response just fine, but when I try to keep file2.php running, file1.php never receives the response, what I am doing wrong? Is there a different way I must send the response back to file1.php?

// file1.php
    $url = "file2.php"

    $params = array('compurl'=>$compurl,'validatecode'=>$validatecode);

    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => true,     // 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_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); 
    curl_close($ch);
    echo $response;

// file2.php
ob_start();
echo 'Running in the background.';

// get the size of the output
$size = ob_get_length();

header("HTTP/1.1 200 OK"); // I have tried without this
header("Date: " . date('D, j M Y G:i:s e')); // Tried without this
header("Server: Apache"); // Tried without this
header('Connection: close');
header('Content-Encoding: none');
header("Content-Length: $size");
header("Content-Type: text/html"); // Tried without this

// flush all output
ob_end_flush();
ob_flush();
flush();

// If I add return; here file1.php gets the response just fine
// But I need file2.php to keep processing stuff and if I remove the
// return; file1.php never gets a response.
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183

1 Answers1

4

In a normal curl transfer you wouldn't be able to get the data until the page has completed loading ie. your script is finished. If you want to work with partial data, you should look at CURLOPT_WRITEFUNCTION . This creates a callback which you can use whenever any data is available.

Ravi
  • 727
  • 6
  • 19
  • But in another stackoverflow question it is indicated that this is possible: http://stackoverflow.com/questions/2996088/how-do-you-run-a-long-php-script-and-keep-sending-updates-to-the-browser-via-http – Serj Sagan Aug 26 '11 at 11:04
  • I tried CURLOPT_WRITEFUNCTION but it still did't read any response.. How can I loop so that it waits for this partial response and doesn't quit the script, but also doesn't tie up resources? – Serj Sagan Aug 26 '11 at 11:11
  • Curl is not a browser. Your browsers can display partial data but curl will only return after the page is done. Frankly I cannot imaging how a function could return partial data unless you are using multi threading. To use partial data, you need a callback which CURLOPT_WRITEFUNCTION provides. – Ravi Aug 26 '11 at 11:11
  • If you need this kind of behavior i.e. fetching and processing simultaneously you would be better off writing your application in a multi threaded language. – Ravi Aug 26 '11 at 11:13