How can I know the status of a call - Asterisk and PHP?
I'm developing an application with PHP and a Asterisk Server. One of the features of the application is to check the call sta开发者_开发百科tus (ringing, answered, hung...) of an specific caller ID, so I would like to know how to do this, because I'm trying with a socket and the command status but I think it has to be a better way to do it.
Thanks in advance.
What API are you using to write this program? AMI? AGI? FastAGI? DeadAGI? Call files? There's a lot of ways to get this information.
NOTE: You cannot tell if a channel has been hungup unless you are using call files and/or CDR access.
With the AMI, you can use the Status command (documented here: http://www.voip-info.org/wiki/view/Asterisk+Manager+API+Action+Status).
With the AGI, you will know the status as it is determined by your program. If you are still in your AGI script, then the call is still in progress and already answered.
If you are using call files, and you have the Archive attribute set to 'yes', then once the call has completed then you can check your outoing_done directory (typically /var/lib/asterisk/outgoing_done/) for your call file. When you read the callfile, you'll notice that Asterisk has appended a status at the bottom of the call file, which will tell you the final status of the call.
The BEST way to get this information is by having your PHP script read from the CDR records on your Asterisk server. Have your CDR records log to a MySQL database, then pull records for your call from the database.
Harph,
You can create an AMI daemon to listen to AMI events streamed from Asterisk. I've done this many times, one of those being for the Asterisk GUI. If you create a table for status in your database, you can create a daemon to listen to the AMI events and update the status as they happen. Then your webpage won't have to interact with Asterisk at all, it'll just need to read the status from the database.
The following link is from the Asterisk GUI project. It is written in javascript, so your php would be a little different, but this will give you a good base to start with. Start with line 574.
http://svn.asterisk.org/view/asterisk-gui/branches/2.0/config/js/welcome.js?view=markup
One of the key differences in javascript vs php is that javascript has to use Asterisk's http daemon instead of a socket connection straight to AMI. Because of this it has to use the 'waitevent' stuff. But since you'll be using a TCP socket connection, just keep looping a read statement.
Hope this helps! This method seems to be the most reliable when trying to get information from Asterisk.
I actually use HTML5 webSockets and AsterClick ,a middleware daemon I wrote in PHP.
AsterClick is , as far as I know ,the only truly event driven Asterisk AMI interface for HTML5 on the planet. All the other solutions out there are still stuck polling the server.
I can watch the status of calls in real time, originate . transfer between (queues,meetme,parking,individuals) simply by drag-n-drop in my browser. I can create functioning ad hoc dialplan entries without writing to the dialplan or basically anything else Asterisk AMI can do with NO POLLING ever.
In Javascript I can use an addEventListener() function that actually takes the names of AMI events as parameters. There is NO POLLING , but rather when the Asterisk AMI emits an event it is sent to my browser over the webSocket generating an event which processes the data and emits more events within the browser.
I also have a tool called WBEA that allows me to deploy those same HTML5 AsterClick applications as stand alone desktop executables for Windows and Linux.
I use CLI command to check channel status.Here is the php code I use
<?php
$socket = fsockopen("111.11.11.111",$portno, $errno, $errstr, $timeout);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: username\r\n");
fputs($socket, "Secret: secret\r\n\r\n");
$channel=$_GET['value']; //pass channel through GET method
echo"<pre>";
fwrite($socket, "Action: Status\r\n");
fwrite($socket, "Command: Lists channel status ".$channel."\r\n\r\n");
$wrets="";
fputs($socket, "Action: Logoff\r\n\r\n");
while (!feof($socket)) {
$wrets .= fread($socket, 8192).'</br>';
}
echo $wrets."<br/>";
fclose($socket);
?>
精彩评论