I trying to get an PHP AGI script work, but it seems the values are not passed back to Asterisk using the methods I have found in the documentations pages, May I ask if some of you gurus, have recently played with AGI and PHP and if you a have a very simple php script that set a variable and passed to Asterisk?
This is the way I am using my Script:
#!/usr/bin/php -q
<?php
require(‘phpagi.php’);
include(‘config.php’);
ob_implicit_flush(true);
set_time_limit(6);
error_reporting(0);
$in = fopen(“php://stdin”,“r”);
$stdlog = fopen("/var/log/asterisk/agi.log", “w”);
// Habilita modo debugging (mas verbose)
$debug = true;
// Hacer las definiciones de funciones antes de empezar el bucle principal
function read() {
global $in, $debug, $stdlog;
$input = str_replace("\n", “”, fgets($in, 4096));
if ($debug) fputs($stdlog, “read: $input\n”);
return $input;
}
function write($line) {
global $debug, $stdlog;
if ($debug) fputs($stdlog, “write: $line\n”);
echo $line."\n";
}
// Colocamos headers AGI dentro de un array
while ($env=read()) {
$s = split(": “,$env);
$agi[str_replace(“agi_”,”",$s[0])] = trim($s[1]);
if (($env == “”) || ($env == “\n”)) {
break;
}
}
// Numero de cuenta y pin enviado desde el telefono
$nrocuenta=$argv[1];
$pin=$argv[2];
// To protect MySQL injection (more detail about MySQL injection)
$nrocuenta = stripslashes($nrocuenta);
$pin = stripslashes($pin);
$nrocuenta = mysql_real_escape_string($nrocuenta);
$pin = mysql_real_escape_string($pin);
$sql=“SELECT Balance FROM tblbalance WHERE Cliente=’$nrocuenta’ and Pin=’$pin’”;
$queryresult=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($queryresult);
// balance matched $nrocuenta and $pin, table row must be 1 row
if($count==1){
// dice el balance resultado del query de la DB
while($balance=mysql_fetch_row($queryresult))
{
$valor = “$balance[0]”;
}
write(“EXEC SayDigits " $valor “\n”);
write(“set variable ScriptResult”$valor”\n");
read();
fclose($in);
fclose($stdlog);
exit;
}
else {
echo “Wrong Username or Password”;
}
?>
My Dial Plan is this:
[ConsultaBalance-inbound]
exten => 4005,1,Answer()
exten => 4005,2,Read(nrocontrato,numerocuenta,4,1,5)
exten => 4005,n,Read(pin,nropin,4,1,5)
exten => 4005,n,Set(dnrocontrato=${nrocontrato})
exten => 4005,n,Set(dpin=${pin})
exten => 4005,n,AGI(’/var/lib/asterisk/agi-bin/consultabalance.php’,${dnrocontrato},${dpin})
exten => 4005,n,Set(elbalance=${ScriptResult})
exten => 4005,n,SayNumber(${elbalance},m)
exten => 4005,n,Hangup()
[ConsultaBalance-outbound]
exten => 4005,1,Answer()
exten => 4005,n,Read(nrocontrato,numerocuenta,4,1,5)
exten => 4005,n,Read(pin,nropin,4,1,5)
exten => 4005,n,Set(dnrocontrato=${nrocontrato})
exten => 4005,n,Set(dpin=${pin})
exten => 4005,n,AGI(’/var/lib/asterisk/agi-bin/consultabalance.php’,${dnrocontrato},${dpin})
exten => 4005,n,Set(elbalance=${ScriptResult})
exten => 4005,n,SayNumber(${elbalance},m)
exten => 4005,n,Hangup()
The ScriptResult variable always has 0 as but if I run the script via shell, it returns 1000, or any valid value.
I had tried to set a fixed value to the ScriptResult variable, but any way when it gets to asterisk its 0.
Any help will be appreciated
Thanks