I have a code which runs a light background task after returning an ok 200 response to the request.
The code runs fine on most apache servers and it was running on heroku but stopped recently, the response now is returned after the whole code is ran.
Any fix ???
ob_start();
ignore_user_abort(); // optional
echo ('{"text": "json Response message to the user".}'); // JSON Response
header('Content-Length: ' . ob_get_length());
header('Content-Type: '.'application/json');
header("Connection: close");
ob_end_flush();
ob_flush();
flush();
session_write_close(); // Added a line suggested in the comment
// Background task here
sleep(1);
echo('Response the user will never see');
for ($i=0; $i<12; $i++) {
sleep(1);
}