Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Originate a call from Asterisk using PHP and Asterisk Manager Interface
/**
* Once this scipt is executed it will connect to the local port you have assigned to
* Asterisk (default: 5038) and send an authentication request. If successfull, it will
* send a second request to originate your call.
*
* The internal SIP line $internalPhoneline will be dialed, and when picked up the
* $target phone will be dialed using your outbound calls context ($context).
*
* Of course, you can modify the commands sent to the asterisk manager interface to suit your needs.
* you can find more about the available options at:
*
* http://www.voip-info.org/wiki/view/Asterisk+manager+API
* http://www.voip-info.org/wiki/view/Asterisk+Manager+API+Action+Originate
*
* Hope this helps!
*
* Licence:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
<?php
// Replace with your port if not using the default.
// If unsure check /etc/asterisk/manager.conf under [general];
$port = 5038;
// Replace with your username. You can find it in /etc/asterisk/manager.conf.
// If unsure look for a user with "originate" permissions, or create one as
// shown at http://www.voip-info.org/wiki/view/Asterisk+config+manager.conf.
$username = "user";
// Replace with your password (refered to as "secret" in /etc/asterisk/manager.conf)
$password = "pass";
// Internal phone line to call from
$internalPhoneline = "203";
// Context for outbound calls. See /etc/asterisk/extensions.conf if unsure.
$context = "context";
$socket = stream_socket_client("tcp://127.0.0.1:$port");
if($socket)
{
echo "Connected to socket, sending authentication request.\n";
// Prepare authentication request
$authenticationRequest = "Action: Login\r\n";
$authenticationRequest .= "Username: $username\r\n";
$authenticationRequest .= "Secret: $password\r\n";
$authenticationRequest .= "Events: off\r\n\r\n";
// Send authentication request
$authenticate = stream_socket_sendto($socket, $authenticationRequest);
if($authenticate > 0)
{
// Wait for server response
usleep(200000);
// Read server response
$authenticateResponse = fread($socket, 4096);
// Check if authentication was successful
if(strpos($authenticateResponse, 'Success') !== false)
{
echo "Authenticated to Asterisk Manager Inteface. Initiating call.\n";
// Prepare originate request
$originateRequest = "Action: Originate\r\n";
$originateRequest .= "Channel: SIP/$internalPhoneline\r\n";
$originateRequest .= "Callerid: Click 2 Call\r\n";
$originateRequest .= "Exten: $target\r\n";
$originateRequest .= "Context: $context\r\n";
$originateRequest .= "Priority: 1\r\n";
$originateRequest .= "Async: yes\r\n\r\n";
// Send originate request
$originate = stream_socket_sendto($socket, $originateRequest);
if($originate > 0)
{
// Wait for server response
usleep(200000);
// Read server response
$originateResponse = fread($socket, 4096);
// Check if originate was successful
if(strpos($originateResponse, 'Success') !== false)
{
echo "Call initiated, dialing.";
} else {
echo "Could not initiate call.\n";
}
} else {
echo "Could not write call initiation request to socket.\n";
}
} else {
echo "Could not authenticate to Asterisk Manager Interface.\n";
}
} else {
echo "Could not write authentication request to socket.\n";
}
} else {
echo "Unable to connect to socket.";
}
@ashishvarma04

This comment has been minimized.

Copy link

@ashishvarma04 ashishvarma04 commented Apr 7, 2017

Sir , i used this code and tried to originate a call , it worked , but i am having an issues with one way audio .. the person 2 cannot hear person1 but person 1 can hear himself as well as person 2.. thanks for your kind help..

@anpel

This comment has been minimized.

Copy link
Owner Author

@anpel anpel commented Jul 11, 2017

@ashishvarma04 Sorry for the late reply, I did not get a notification for your comment.

Did you manage to fix your issue? Please let me know so I can update this gist as needed.

@pradipkilledar

This comment has been minimized.

Copy link

@pradipkilledar pradipkilledar commented Feb 23, 2018

Anpel Sir,

Thanks you very much, it works,

I am trying to get all the events on the web page like
once dialing ,ringing,Answer,hangup real time, could you please suggest me to do that ,

@mesutd0nmez

This comment has been minimized.

Copy link

@mesutd0nmez mesutd0nmez commented Apr 10, 2018

If you live problem in call between 2 extensions, change context to from-internal. Then it will work.

@heron-brito

This comment has been minimized.

Copy link

@heron-brito heron-brito commented Apr 15, 2018

These scripts is working fine. It's missing only the target variable definition.

@Telegramsrv

This comment has been minimized.

Copy link

@Telegramsrv Telegramsrv commented May 2, 2018

Could not initiate call. why???
call_not

@crespo5

This comment has been minimized.

Copy link

@crespo5 crespo5 commented May 24, 2018

@Telegramsrv try to put this behin originate requests definition: $originateRequest .= "Variable: CALLERID(number)=$extension\r\n"; where $extension is your country extension (USA is extension 0001 e.g.)

If this failed, try to change $originateRequest .= "Callerid: Click 2 Call\r\n"; for $originateRequest .= "Callerid: $target\r\n"; where $target is the number.

Good Luck!

@huzoorbux

This comment has been minimized.

Copy link

@huzoorbux huzoorbux commented Apr 2, 2019

How can i make a call on a toll free IVR number and and send input to that ivr.

@nanotech-dot

This comment has been minimized.

Copy link

@nanotech-dot nanotech-dot commented Jan 18, 2020

hello
i have to test this script this work as well on asterisk 1.8 but once im trying with asterisk 16 its not work, please help me.

@rdadolfo

This comment has been minimized.

Copy link

@rdadolfo rdadolfo commented Apr 2, 2020

Thank you for your script. It's working in Asterisk 16.9.0. God bless you.

@anpel

This comment has been minimized.

Copy link
Owner Author

@anpel anpel commented Apr 2, 2020

Thank you for your script. It's working in Asterisk 16.9.0. God bless you.

Have not tested it in a while, good to know it still works.

Thank you and have a great day.

@arijit-pal

This comment has been minimized.

Copy link

@arijit-pal arijit-pal commented Jul 3, 2020

Thanks for the code, This snippet is working fine to make a cal on one side. I am not able to hear any sound while receiving the call. Also after going through Asterisk server documentation I found that I need to get the response on receiving a call. Based on the call 'answered' and the related response, I need to originate the same call function to connect dialer. This can only help to make the call two directional,
Please advice

@sigmund-avila

This comment has been minimized.

Copy link

@sigmund-avila sigmund-avila commented Oct 6, 2020

Hi Anpel!

Im creating an Incoming call pop up window for the CRM that I created. I successfully manage to view the AMI to my server using telnet. Now my problem is how can I listen to the pbx incoming events. I want to hook every time there are incoming call then I will push a notification to my users so they can see the information of the caller.

Thanks,
Sigmund

@anpel

This comment has been minimized.

Copy link
Owner Author

@anpel anpel commented Oct 6, 2020

@sigmund-avila It's been a while since I last worked with Asterisk so I can't be too helpful.

Seems like someone was trying to do the same here: https://community.asterisk.org/t/solved-trigger-page-on-incoming-call/38767.

I see some code samples and the issue is marked as solved, so I would start with that.

Good luck!
Andreas

@Trenipuccio

This comment has been minimized.

Copy link

@Trenipuccio Trenipuccio commented Jan 26, 2021

immagine

Hi, when i try to call the internal 333 from internal 240, i have the error in the pic.

Any suggestion to resolve it?

@Trenipuccio

This comment has been minimized.

Copy link

@Trenipuccio Trenipuccio commented Jan 27, 2021

Well, I solved myself, i need to put $context = "default"; in the code.

@aezzouhri

This comment has been minimized.

Copy link

@aezzouhri aezzouhri commented Mar 3, 2021

Well, I solved myself, i need to put $context = "default"; in the code.

Hello, please for calls coming from outside to Asterisk. What should we put in context ?

@Trenipuccio

This comment has been minimized.

Copy link

@Trenipuccio Trenipuccio commented Mar 4, 2021

Well, I solved myself, i need to put $context = "default"; in the code.

Hello, please for calls coming from outside to Asterisk. What should we put in context ?

Really don't know, we only use for only internal calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment