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 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).

[code]<?php
$BASE_PATH="/var/lib/asterisk/agi-bin/";
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.’;
die(100);
}
echo ‘debug 1’;
$astManager->add_event_handler(’*’,‘evt_all’);
$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
$astManager->disconnect();

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

If this were in perl I’d be whipping out some black magic $|=1; but I haven’t done enough PHP to know how to unbuffer standard out.

A quick google without testing any code suggested fflush(stdout); after each echo. (I’m not sure that’s even PHP at this point!)

YMMV, but buffering sounds like the culprit.

Ok, thats helpful… looking at php manual I see there is a flush command and ob_flush command.

I will play with those and see where I get!

Thanks

I would recommend having a look at the php-asmanager class that freepbx uses.

After a whole day of ‘playing’ I came up with a script that continuously displays the events being sent by AMI.

You can find it here: Scroll down to the comment titled AMI EVENT READER:
http://www.freepbx.org/forum/freepbx/general-help/how-to-find-current-hold-time-of-an-extension