The AGI_AsteriskManager class provides an API to the Asterisk Manager.
AGI_AsteriskManager is not part of the current release version
1.12, it is only available from CVS.
AGI_AsteriskManager should be considered experimental. Most notably
the asm_XXXXXXXX application functions are not fully tested, or even
implemented.
Note: This documentation is rough, just in time for the holidays.
An example script, as-test.phps runs as an AGI
within Asterisk, connects to the local manager and 1) pings once a second,
and 2) dumps events from the manager.
CREATING A NEW INSTANCE OF THE CLASS
The class can be created standalone of phpagi.php, or through phpagi.
require "phpagi-asmanager.php";
$as = new AGI_AsteriskManager();
FROM PHPAGI:
require "phpagi.php";
$agi = new AGI();
$as = $agi->new_asm();
Notes:
- If the class is created using $agi->new_asm(), AGI_AsteriskManager will use
phpagi for logging to the Asterisk console.
- phpagi.php will include phpagi-asmanager.php by itself.
- if phpagi-asmanager.php is included _before_ phpagi.php, phpagi.php will not attempt to re-include it.
- if phpagi.php tries to include phpagi-asmanager.php but is unable to do so,
an<
error will be echoed to the asterisk console and the script will continue running
normally. in this case the return value of new_asm() will be
FALSE.
CONFIGURATION
phpagi-asmanager uses the same configuration file as phpagi.conf (usually /etc/asterisk/phpagi.conf).
All configuration information specific to phpagi-asmanager is contained in the [asmanager] section
of the .conf file.
(note: not implemented)
supported directives:
[asmanager]
# default manager port
port=1234
CONNECTING
$res=$as->connect("localhost","username","password");
if($res==FALSE) {
echo "Connection failed.\n";
}
if($res==TRUE){
echo "Connection established.\n";
}
A port can also be specified for the hostname. eg:
$res->$as->connect("my.asterisk.server:1234","username","port");
if the port is not specified, the default manager port will be used
instead.
SENDING REQUESTS
$as->send_request($eventname,$arrayofparameterstopass);
send_request() calls wait_request and returns an array of returned data from the
manager. if something went wrong, it returns false.
wait_request() shouldn't need to be called from a script directly.
wait_request() will also detect events and dispatch any registered event handlers
for the event.
examples:
$res=$as->send_request("EventName",
array(
"Channel"=>"Zap/1/16045551212",
"AnotherParameter"=>"data"
)
);
if($as->resp_is_success($res)){
echo "it worked!";
}
if($as->resp_is_error($res)){
echo "it failed!\n";
}
echo "Dump of returned data:\n";
foreach($res as $var=>$val){
echo "$var = $val\n";
}
EVENTS
- TODO: non-blocking socket i/o.
- The class uses event callbacks to process events received from the
manager.
event callback prototype looks like:
function dump_event($ecode,$data,$server,$port)
{
echo "received event '$ecode' from $server:$port\n";
print_r($data);
}
to register an event call back:
$as->add_event_handler($eventname,"eventfunction");
eg:
$as->add_event_handler("registry","dump_event");
the special eventname "*" can also be registered. any eventname not specifically
registered will be handled by the "*" handler. if no * handler is defined, the
event will be silently ignored.
PRECANNED FUNCTIONS
Undocumented..
|