handling AMI using phpagi-asmanager

AMI + phpagi-asmanager

Here is one example of how you can use Asterisk Manager Interface to retrieve live information from you Asterisk Server. In this example I show how to retrieve all active channels and the elapsed time for each channel:


#!/usr/bin/php -q

require_once "phpagi.php";
require_once "phpagi-asmanager.php";

$AGI = new AGI();

if(!isset($_SERVER['argv'][1]))
{
debug("Usage:t{$_SERVER['_']} {$_SERVER['argv'][0]} peernn",1);

exit;
}

$ampmgruser  = get_var( $AGI, "AMPMGRUSER" );
$ampmgrpass  = get_var( $AGI, "AMPMGRPASS" );

$astman = new AGI_AsteriskManager();
if ($astman->connect("127.0.0.1",$ampmgruser,$ampmgrpass))

{

$peer = $astman->command("sip show peer {$_SERVER['argv'][1]}");
if(!strpos($peer['data'], ':')){
debug("****step1****",1);
echo $peer['data'];
}
else
{
$data = array();
foreach(explode("n", $peer['data']) as $line)
{
$a = strpos('z'.$line, ':') - 1;
if($a >= 0) $data[trim(substr($line, 0, $a))] = trim(substr($line, $a + 1));
}
debug("extension 106 status is: *** ".$data['Status']." ***",1);
// print_r($data);
}

debug("****".$peer['data'],1);

$astman->disconnect();
}

exit( 0 );

function get_var( $agi, $value) {
$r = $agi->get_variable( $value );

if ($r['result'] == 1) {
$result = $r['data'];
return $result;
}
return '';
}

function is_ext_avail( $extnum ) {
global $astman;

$status = $astman->ExtensionState( $extnum, 'ext-local' );

$status = $status['Status'];
debug("ExtensionState: $status", 4);
return $status;
}

function debug($string, $level=3) {
global $AGI;
$AGI->verbose($string, $level);
}

?>

You may also like