Transfer-Encoding: chunked header in PHP. What is padding for?
The padding is for filling the server buffer, as I understand.
Without it the server will wait until PHP will fill it and after this will flush it - even if in PHP code you do flush()
.
Related:
- PHP Output buffer flush issue on Apache/Linux (May 2010; on Server Fault)
Transfer-Encoding: chunked sent twice (chunk size included in response body)
As @Joe pointed out before, that is the normal behavior when Chunked transfer enconding is enabled. My tests where not accurate because I was requesting Apache directly on the server. Actually, when I was experiencing the problem in Chrome I was querying a Nginx service as a proxy for Apache.
By running tcpdump
I realized that Nginx was rechunking responses, but only when rewritting HTTP status header (header('HTTP/1.1 200 OK')
) in PHP. The solution to sending Transfer-Encoding: chunked
twice is to set chunked_transfer_encoding off
in the location
context of my Nginx .php handler.
How can I use chunked transfer encoding in Laravel?
In my specific case I've solved my problem by using this code(example):
use Symfony\Component\HttpFoundation\StreamedResponse;
$response = new StreamedResponse();
$response->setCallback(function () {
var_dump('Hello World');
flush();
sleep(2);
var_dump('Hello World');
flush();
});
$response->send();
POST request to PHP7 with chunked encoding does not properly return result
It is/was a bug in PHP7 which was fixed very recently with https://github.com/php/php-src/pull/1745 . It is not yet within any official release but eventually will end up there some time.
Before the above PR, a bug was also reported describing a similar issue: https://bugs.php.net/bug.php?id=71466
know when data chunk transfer is closed
By calling connection_aborted()
you can find out if the client is still connected or not. This could be done within your for
loop like below.
Also you will need to tell PHP to continue execution after the client disconnects by calling ignore_user_abort()
. If you don't do that, your script will terminate as soon as the client disconnects. Depending on your PHP config, you may also need to set a larger or unlimited time limit using set_time_limit()
.
ignore_user_abort(true);
for ($i = 0; $i < 10000; $i++) {
if(connection_aborted()){
// client disconnected, write to file here
}
echo "string";
ob_flush();
flush();
sleep(2);
}
Related Topics
PHP - Indirect Modification of Overloaded Property
How to Check If $_Get Is Empty
Directory Index Forbidden by Options Directive
Ssl Error Ssl3_Get_Server_Certificate:Certificate Verify Failed
Php: How to Sort the Characters in a String
How to Ceil, Floor and Round Bcmath Numbers
How to Replace a Function in PHP (Such as Mail) and Make It Do Something Else
MySQL Select Query Within a Serialized Array
How to Use Single Quote Inside an Echo Which Is Using Single Quote
Adding Custom Field to Product Category
How to Access PHP Session Variables from Jquery Function in a .Js File
Can Someone Explain the /E Regex Modifier
What Does This Mean? "Parse Error: Syntax Error, Unexpected T_Paamayim_Nekudotayim"