Menu

Make php script return continuous events?

jsherk
2011-05-30
2013-04-29
  • jsherk

    jsherk - 2011-05-30

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

    <?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 '<br>'.$ecode.' - '.print_r($data,1);
    }
    ?>
    
     
  • Jack

    Jack - 2012-04-15

    The trick is to loop but not sleep since while sleeping you ain't receiving anything.
    Just do the following

    while(true){
        $ami->wait_response(true);
    }
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.