how can I get php real-time browser output-ThrowExceptions

Exception or error:

All the answers to similar questions I’ve found here are many years old and don’t help me.
I’ve tried all sorts of suggestions I’ve found online and in the php manual but all give the same result: no output in the browser until the php script finishes executing.
This is the most recent code I’ve tried:

<?php
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);

//Flush (send) the output buffer and turn off output buffering
//ob_end_flush();
while (ob_end_flush());

// Implicitly flush the buffer(s)
ini_set('implicit_flush', true);
ob_implicit_flush(true);

//prevent apache from buffering it for deflate/gzip
header("Content-type: text/plain");
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

for($i = 0; $i < 1000; $i++)
{
echo ' ';
}

ob_flush();
flush();

/// Now start the program output

echo "Program Output";

ob_flush();
flush();
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo $i . '\n<br>';
    ob_flush();
    flush();
    sleep(1);
}
echo 'End ...<br>';
?>

How can I get real time output in the browser?
Thanks in advance!

How to solve:

You need to look at either front-end polling of an endpoint or setting up streaming through something like websockets. The simple explanation is that browsers wait for the response from your webserver which does not complete until the php script is complete.

Leave a Reply

Your email address will not be published. Required fields are marked *