PHP Curl output buffer does not receive a response

I have a deal in which file1.php curl launches file2.php. file2.php is a long file, but it sends (or should send) the response back to file1.php, and then continues the code with it. I use the output buffer to try to send this data, but the problem is that I will return; immediately after a flush; file1.php gets the answer just fine, but when I try to save file2.php, file1.php never gets the answer, what am I doing wrong? Is there any other way to send a response 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 MYG: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. 
0
source share
1 answer

With the usual transfer of the cursor, you cannot get the data until the page finishes loading, i.e. your script is finished. If you want to work with partial data, you should look at CURLOPT_WRITEFUNCTION. This creates a callback that you can use whenever any data is available.

+3
source

Source: https://habr.com/ru/post/1385662/


All Articles