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

Topic: Sample PHP script for Asterisk AMI integration

    In this article I have provided a sample PHP script to integrate with asterisk via AMI to place call via HTTP GET method, which will be used for click to dial from another CRM or any web portals.

sample php script with asterisk AMI integration

what is Asterisk and 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 AMI is a client/server model over TCP. With the manager interface, you'll be able to control the PBX, originate calls, check mailbox status, monitor channels and queues as well as execute Asterisk commands.

AMI is the standard management interface into your Asterisk server. You configure AMI in manager.conf. By default, AMI is available on TCP port 5038 if you enable it in manager.conf.

PHP script with AMI integration

    If you are looking for a PHP script for click to dial from a CRM or a web page then this is sample script if for you.
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 dialed.
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

Conclusion:

    you can use the above script to integrate with your crm for click to dial functionality,make sure to send proper variable in the HTTP GET API request to the asterisk server or to your PHP webserver.

1 Comments
  • Ajit Kumar
    Ajit Kumar April 4, 2022 at 11:31 AM

    PHP script for click to dial or API to dial from asterisk via http

Add Comment
comment url