Here is my php script::
#!/usr/bin/php -q
<?php
$dbusername = "asterisk";
$dbname = "myphonesystems";
$dbpass = "Asterisk@123";
$dbhost = "127.0.0.1";
// Connect to the database
$conn = mysqli_connect("$dbhost", "$dbusername", "$dbpass", "$dbname");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Import the PHPAGI library
require_once('/var/lib/asterisk/agi-bin/phpagi.php');
// Create an instance of the AGI class
$agi = new AGI();
$userName = $agi->request['agi_arg_1'];
$ext = $agi->request['agi_arg_2'];
$user_id = $agi->request['agi_arg_3'];
$sip_file = "/etc/asterisk/sip.conf";
$file = fopen($sip_file, 'r');
// $user_id = null;
// Read the file line by line
while (($line = fgets($file)) !== false) {
Check if the line starts with the user's name in square brackets
if (preg_match('/^\[' . preg_quote($userName) . '\]/', $line)) {
// If it does, set the $user_data variable
$user_data = $line;
// Extract the user ID from the SIP configuration
if (preg_match('/^user_id=(\d+)/', $line, $matches)) {
$user_id = $matches[1];
}
}
}
fclose($file);
if ($user_id) {
if ($ext == "get_number") {
$extension_query = "SELECT extension_name FROM extensionmps WHERE user_id=?";
$stmt = mysqli_prepare($conn, $extension_query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
$cuser = $row['extension_name'];
$agi->verbose($cuser);
$agi->set_variable("CALLERID(num)", $userName);
$agi->set_variable("CALLERID(name)", $userName);
$agi->set_variable('call_number', $cuser);
}
mysqli_stmt_close($stmt);
}
} else {
$agi->verbose("User with username $userName is not registered");
}
Dial the extension number
$dial = $agi->exec("Dial", "SIP/${call_number}");
// Hang up the call
$agi->exec("Hangup", "");
?>
extension.conf file::
[phones]
exten => _X.,1,NoOp(Entering my-dialplan)
same => n,Set(userName=${CALLERID(num)})
same => n,AGI(call.php,${userName},get_number)
same => n,Dial(SIP/${userName}, 20)
same => n,Hangup()