Hosted Vicidial server starts from $39 Contact Us Buy Now!

Sample PHP script to place call using asterisk AMI over http GET

Estimated read time: 2 min

PHP script to integrate with asterisk AMI to place calls

topic: Sample PHP script to place call using asterisk AMI over http GET
asterisk ami php script

  Introduction: Asterisk - AMI

    Asterisk is a Open source Communication software , You can use Asterisk to build communications applications, things like business phone systems (also known as IP PBXs), call distributors, VoIP gateways and conference bridges.
    The Asterisk Gateway Interface, or AGI, provides a standard interface by which external programs may control the Asterisk dialplan. Usually, AGI scripts are used to do advanced logic, communicate with relational databases (such as PostgreSQL or MySQL), and access other external resources.

  Overview: PHP script with AMI integration

    In this article i have provided a sample php script, which will be used as a http api to place a call from a crm using the Asterisk AMI integration.
you need to send the HTTP request with GET method along with the query parameter name and value as shown below

https://192.168.1.2/call.php?exten=SIP/222&number=9988776655
note:
exten is the extension number or source to be connected
number is the number to be dialled.
It will initially call the extension 222 and then dial the number 9988776655

  Pre-requisites:

    Below are the pre-requisites required to use this script
1. AMI username and password
2. AMI user with call permission
3. PHP installed in server 

AMI user and permission

    create a new AMI user in asterisk with call and originate permission

vi /etc/asterisk/manager.conf

add the below user details and reload asterisk 

[test]
secret = test1234
read = system,call,log,verbose,command,agent,user,originate
write = system,call,log,verbose,command,agent,user,originate

Creating a call.php script in webroot folder

Go to your webroot folder and create a php file with the below php code.
for me the webpath is /var/www/html
cd /var/www/html
vi call.php
Copy paste the below php code and save the file,
<?php
#ip address that asterisk is on.
$strHost = "127.0.0.1";
$strUser = "test";#specify the asterisk manager username you want to login with
$strSecret = "test1234";#specify the password for the above user
#specify the channel (extension) you want to receive the call requests with
#e.g. SIP/XXX, IAX2/XXXX, ZAP/XXXX, etc
# $strChannel = "SIP/100";
$strChannel = $_REQUEST['exten'];
$strContext = "from-internal";
#specify the amount of time you want to try calling the specified channel before hangin up
$strWaitTime = "30";
#specify the priority you wish to place on making this call
$strPriority = "1";
#specify the maximum amount of retries
$strMaxRetry = "2";
$number=strtolower($_REQUEST['number']);
$pos=strpos ($number,"local");
if ($number == null) :
exit() ;
endif ;
if ($pos===false) :
$errno=0 ;
$errstr=0 ;
$strCallerId = "Web Call $number";
$oSocket = fsockopen ("localhost", 5038, &$errno, &$errstr, 20);
if (!$oSocket) {
echo "$errstr ($errno)<br>\n";
} else {
fputs($oSocket, "Action: login\r\n");
fputs($oSocket, "Events: off\r\n");
fputs($oSocket, "Username: $strUser\r\n");
fputs($oSocket, "Secret: $strSecret\r\n\r\n");
fputs($oSocket, "Action: originate\r\n");
fputs($oSocket, "Channel: $strChannel\r\n");
fputs($oSocket, "WaitTime: $strWaitTime\r\n");
fputs($oSocket, "CallerId: $strCallerId\r\n");
fputs($oSocket, "Exten: $number\r\n");
fputs($oSocket, "Context: $strContext\r\n");
fputs($oSocket, "Priority: $strPriority\r\n\r\n");
fputs($oSocket, "Action: Logoff\r\n\r\n");
sleep(2);
fclose($oSocket);
}
echo "Extension $strChannel should be calling $number." ;
else :
exit() ;
endif ;
?>

Followed to that provide the execute permission to the call.php file

chmod 755 call.php

Now execute the file from a browser to test the call 

http://serverip/call.php?exten=SIP/100&number=9999999999

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.