Skip to content

Instantly share code, notes, and snippets.

@tgirardi
Last active April 27, 2018 23:28
Show Gist options
  • Save tgirardi/7a65e3004d13f8e3cbe130cf444bd860 to your computer and use it in GitHub Desktop.
Save tgirardi/7a65e3004d13f8e3cbe130cf444bd860 to your computer and use it in GitHub Desktop.
Example PHP script you can use to initiate calls through HTTP requests using an Asterisk Server. You must install and configure an HTTP server with PHP enabled. Also, read the comments in the file to adjust it correctly and know better what you need to do
// Don't forget to change values for $username and $secret (where it says PUT_YOUR_USERNAME_HERE and PUT_YOUR_PASSWORD_HERE)
<?if (!empty( $_REQUEST['phone']) && !empty( $_REQUEST['exten'] ) )
{
$num = $_REQUEST['phone'];
$ext = $_REQUEST['exten'];
$num = preg_replace( "/^\+7/", "8", $num );
$num = preg_replace( "/\D/", "", $num );
$username = 'PUT_YOUR_USERNAME_HERE';
$secret = 'PUT_YOUR_PASSWORD_HERE';
$timeout = 10;
# IP where asterisk's AMI manager is listenning
# Note: you might need to enable the manager and configure security settings
# in /etc/asterisk/manager.conf (probably also in your firewall)
$asterisk_ip = "127.0.0.1";
# asterisk's AMI manager port
$asterisk_port = "5038";
if ( ! empty( $num ) )
{
echo "Dialing $num\r\n";
$socket = fsockopen($asterisk_ip, $asterisk_port, $errno, $errstr, $timeout);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: " . $username . "\r\n");
fputs($socket, "Secret: " . $secret . "\r\n\r\n");
$wrets=fgets($socket,128);
echo $wrets;
fputs($socket, "Action: Originate\r\n" );
fputs($socket, "Channel: Local/$ext@axia-salida\r\n" );
fputs($socket, "Exten: $num\r\n" );
fputs($socket, "Context: axia-salida\r\n" );
fputs($socket, "Priority: 1\r\n" );
fputs($socket, "Async: yes\r\n" );
fputs($socket, "WaitTime: 15\r\n" );
fputs($socket, "Callerid: $num\r\n\r\n" );
$wrets=fgets($socket,128);
echo $wrets;
}
else
{
echo "Unable to determine number from (" . $_REQUEST['phone'] . ")\r\n";
}
}
else
{?>Please enter a number to dial.
<?}?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment