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");
}

3 comments:

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

Berthold Hanisch said...


Greetings from Ohio! I'm bored at work so I decided to check out your blog on my iphone during lunch break. I love the info you present here and can't wait to take a look when I get home. I'm amazed at how fast your blog loaded on my cell phone .. I'm not even using WIFI, just 3G .. Anyways, wonderful site! sign in hotmail

ajay ak said...

Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
Webdesining course in chennai