PHP AGI values are not passed back to Asterisk

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

Why would you include phpagi and not use the methods provided by this class, but instead read and write directly from and to asterisk? Check out the phpagi class documentation at phpagi.sourceforge.net/phpagi22/api-docs/. For setting dialplan variables you would be interested in the set_variable() method.

Hi, SGM, Thanks for your replay,
Unfortunately I am not as proficient as I like to be in PHP :frowning:, I copy the script from jroliva.wordpress.com/?s=stock and made some customization to fit my environment, any way it seems it does not work any way, I like some samples of how to use the methods. I think I´ve done my homework but with no success.

How should I use this method to get a value pass back to asterisk?

Thanks in advance.

You should use it like:

<?php $agi = new AGI(); // ... do whatever you want $agi->set_variable('variable name', 'value'); // ... you could still do whatever you want ?>

Stoyan

Solved, and Thank you very much,
Just one thing for the inexpert as I I am use the syntax exactly as I write it here

$agi = new AGI();
$agi->set_variable(‘ScriptResult’, $valor);
exit;

do NOT put the $ sing to the first parameter and do NOT put ’ ’ to the second parameter, but use $

Next the code modified and working.

#!/usr/bin/php -q

<?php require('phpagi.php'); include('config.php'); ob_implicit_flush(true); set_time_limit(6); error_reporting(0); // 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,$dbconn); // 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]"; } $agi = new AGI(); $agi->set_variable('ScriptResult', $valor); exit; } else { echo "Wrong Username or Password"; } ?>

Again Thank
:mrgreen:

[quote=“yelmo -jrsanchez-”]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[/quote]