PHP script to continuously display events from AMI?

I am working a php script that I want to record all events (will work on filters later) as they occur. In my script below though, I loop for 5 seconds and then when I disconnect from astManager, but nothing is displayed until AFTER the loop finishes and it disconnects. Even my “debug” text does not display until after.

I need to get realtime (or continuous) events so I can parse them and not miss any. How do I get my script to loop continuously/indefinitely and display (read) the events as they occur?

NOTE: I put script in /var/www/html/ for testing purposes so I can access from my local network over VPN (it is NOT web accessible).

require $BASE_PATH.“phpagi.php”;
$astManager = new AGI_AsteriskManager();
$res = $astManager->connect($ast_host, $ast_user, $ast_pass);
if(!$res) {
syslog(LOG_INFO, “Connection to Asterisk manager failed.”);
//echo ‘Connection to Asterisk manager failed.’;
echo ‘debug 1’;
$start_time = date(‘His’);
$end_time = $start_time + 5; //in seconds
for ($x=$start_time; $x<$end_time; $x=date(‘His’)) { } //LOOP until time runs out
echo ‘debug 2’;
// Now we close the connection and logout

//FUNCTION - Do something with all events
function evt_all($ecode, $data, $server, $port) {
echo ‘
’.$ecode.’ - '.print_r($data,1);

Ok, I ended up writing something I call the AMI EVENT READER.

You can find it here, i think about the 4th post down: