Stream response from CURL request without waiting for it to finish

Check out Pascal Martin's answer to an unrelated question, in which he discusses using CURLOPT_FILE for streaming curl responses. His explanation for handling " Manipulate a string that is 30 million characters long " should work in your case.

Hope this helps!


Yes you can use the CURLOPT_WRITEFUNCTION flag:

curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback);

Where $ch is the Curl handler, and $callback is the callback function name. This command will stream response data from remote site. The callback function can look something like:

$result = '';
$callback = function ($ch, $str) {
    global $result;
    $result .= $str;//$str has the chunks of data streamed back. 
    //here you can mess with the stream data either with $result or $str
    return strlen($str);//don't touch this
};

If not interrupted at the end $result will contain all the response from remote site.


Pass the -N/--no-buffer flag to curl. It does the following:

Disables the buffering of the output stream. In normal work situations, curl will use a standard buffered output stream that will have the effect that it will output the data in chunks, not necessarily exactly when the data arrives. Using this option will disable that buffering.

Note that this is the negated option name documented. You can thus use --buffer to enforce the buffering.

Tags:

Php

Curl