PHP live peer status

Hi,
is there any nice way to show peer status in a PHP script. So that someone can see if user xy is online or available? I’ve seen some attends but not really nice things. For example use the output of the console commands. I’m thinking about a MySQL database where every peer is regsitered as soon as he goes online.

Thank you in advance

The easiest way is to use asterisk -x “sip show peers” command with a combination of :. grep , awk or any other Linux’s filtering tool, then a cron job. But a more sophisticated way is use the AMI actions and events related to peer status, and the PHP fsockopen() function , this will add realtime information and speed

1 Like

Direct method:

$peers = AMI_exec('sip show peers');  
function AMI_exec($cmd){
       $am_login = 'admin';
       $am_pass = 'amp111';
        $socket = fsockopen("127.0.0.1","5038", $errno, $errstr, 10);
        if ( !$socket ){ echo "Failed  $errstr ($errno)\n"; }
        fputs($socket, "Action: Login\r\n");
        fputs($socket, "UserName: {$am_login}\r\n");
        fputs($socket, "Secret: {$am_pass}\r\n\r\n");
        usleep(500);
        fputs($socket, "Action: Command\r\n");
        fputs($socket, "Command: {$cmd}\r\n\r\n");
        usleep(500);
        fputs($socket, "Action: Logoff\r\n\r\n");
        usleep(500);
        $result=fgets($socket,128);
        fclose($socket) ;
        return $result;
}

Method via Mysql Database ( Realtime config using table “sip”):

SELECT (CASE
WHEN IFNULL(sip.ipaddr,’’) = ‘’ THEN ‘OFFLINE
WHEN ( (regseconds - UNIX_TIMESTAMP() ) > 0) THEN concat(’  ONLINE  [ttl:’,(regseconds - UNIX_TIMESTAMP() ),’]’)
WHEN ( (regseconds - UNIX_TIMESTAMP() ) < 10000) THEN ‘OFFLINE
WHEN ( (regseconds - UNIX_TIMESTAMP() ) < 100) THEN concat(’ REG EXPIRED[’,regseconds - UNIX_TIMESTAMP(),‘ms]’)
END) AS chan_reg_status
FROM sip

1 Like