Asterisk server is used through soft phone to make a call between two users. Asterisk Gateway Protocol (AGI) is the protocol used as an interface for telephony applications. This code ensures that we have open file handles for STDIN, STDOUT and STDERR, which will manage all communication between Asterisk and the script.
#!/usr/bin/php -q
<?
ob_implicit_flush(false);
set_time_limit(6);
$stdin = fopen('php://stdin', 'r');
$stdlog = fopen('my_agi.log', 'w');
$debug = false;
/* Read input from Asterisk and output via $astOutput */
function astRead()
{
global $stdin, $debug, $stdlog;
$astOutput = str_replace("n", "", fgets($stdin, 4096));
if ($debug) fputs($stdlog, "read: $inputn");
return $astOutput ;
}
/* Write AGI command to Asterisk */
function astWrite($agiCommand)
{
global $debug, $stdlog;
if ($debug) fputs($stdlog, "write: $agiCommandn");
echo $agiCommand."n";
}
/* Handling execution input from Asterisk */
$agivar = array();
while (!feof($stdin))
{
$temp = fgets($stdin);
$temp = str_replace("n","",$temp);
$s = explode(":",$temp);
$agivar[$s[0]] = trim($s[1]);
if ($temp == "")
{
break;
}
}
/* Operational Code starts here */
/* Playback the demo-congrats.gsm file from the
* directory /var/lib/asterisk/sounds/
*/
astWrite("STREAM FILE demo-congrats #");
astRead();
/* Say the number 123456 */
astWrite("SAY NUMBER 123456 #");
astRead();
/* Finalization of AGI script and clean-ups */
fclose ($stdin);
fclose ($stdlog);
exit(0);
?>