Ok, I've searched everywhere to find a solution but I guess no one has had this problem, it's pretty weird.
I have a script where I make a request to another website using Curl, and parse the response to extract some specific info (CURLOPT_RETURNTRANSFER is set to TRUE). All this is in a loop, which takes some time to complete, and I want to be able to show the progress as it is happening, something like this:
information 1: "extracted info 1"
information 2: "extracted info 2"
information 3: "extracted info 3"
...
The problem is that it's not sending the results as it gets them, but just shows it all at once after it's finished)
Here's my code (simplified/removed unrelevant parts):
<?php
for($number=0;$number<10000;$number++)
{
//curl
$ch = curl_init();
$url = "http://www.website.com/page.php?info=$number"; //just so you get how I'm using use it;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$answ = curl_exec($ch);
curl_close ($ch);
/*...
here I parse $answ and put the string I'm looking for into $data
... */
//after that I want to show the newest information I obtained
echo "information $number: $data<BR>";
flush();
}
?>
I think I have found the line which causes the problem, but still don't know how to fix this. The line is:
$answ = curl_exec($ch);
If I remove this line, it works. Well, at least the buffer-flushing part does, since I never make the call to the website and only get this:
information 1:
information 2:
information 3:
...
But you can see that it gets sent to the browser as the script is still being executed.
I also tried using ob_start() and ob_flush() instead of flush(), with the exact same results.
Is this a bug in the Curl library or something? I could really use some help because I have no idea what is wrong.
Nicolas