Translate

2013-04-16

PHP script using the Asterisk manager to place a call between two parties

In brief, what is the manager ?

One of the ways to use Asterisk is opening a socket with its manager (by default running on port TCP/5038, can be configured using manager.conf).

When the socket is opened, it is possible to authenticate (Action:Login) and to send commands that Asterisk will treat (see here the full API), it allows also to follow the live logs of Asterisk activity, can be very useful please see here a possible application.

A good exercise is to connect to the manager using telnet and just follow the activity.

What I want to present here is a simple PHP script openning a socket with the manager and sending commands to establish a call between two parties;

The Code

(this page is called from a form that supplies the two extensions to connect, the code is simplified to ease the comprehension);



if($_POST['lancer'])
{
$called=$_POST['extension'];
$caller=$_POST['sipuser'];
echo "$caller you will be connected to ".$called;

// connexion to manager opening a socket ans authenticating wiant an user defined in mamanger.conf
$socket = fsockopen("xxxxxx","5038", $errno, $errstr);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: superuser\r\n");
fputs($socket, "Secret: xxxxxx\r\n\r\n");
fgets($socket);

// Call launch
fputs($socket, "Action: Originate\r\n");
fputs($socket, "Exten: $called\r\n");
fputs($socket, "Context: yyyyyyy\r\n");
fputs($socket, "CallerID: $called\r\n");
fputs($socket, "Priority: 1\r\n");
fputs($socket, "Channel: SIP/$caller\r\n\r\n");

//Disconnection
fputs($socket, "Action: Logoff\r\n");
}

1 comment:

Stefan Forge said...


Hello! Quick question that's completely off topic. Do you know how to make your site mobile friendly? My weblog looks weird when viewing from my iphone. I'm trying to find a theme or plugin that might be able to fix this issue. If you have any recommendations, please share. Many thanks! paypal login my account