SOLVED Passing variable to dialplan from AGI

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
I can't seem to get this working using PIAF with asterisk v1.8.x. I've set up a very simple test to try and pass a variable within the dialplan using a simple test.agi file, viewing the result using NoOP.....

Dialplan
Code:
exten => _X.,n,AGI(/var/lib/asterisk/agi-bin/test.agi)
exten => _X.,n,NoOp(variable value=${ScriptResult})

test.agi
Code:
#!/usr/bin/php
<?php
 
    $vartest= 'value';
    $agi = new AGI();
    $agi->set_variable('ScriptResult', $vartest);
?>

I've googled around and just can't seem to get it going. Thanks for any help or pointers!
 

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,206
Reaction score
5,229
Here's what works for me. With this function, you can pass variables in both directions.

Dialplan:
Code:
exten => 1234,1,AGI(test.agi)
exten => 1234,n,NoOp(${ScriptResult})
exten => 1234,n,Hangup

test.agi:
PHP:
#!/usr/bin/php -q
<?php
 
$debug=0;
$stdlog = fopen($log, 'a');
$stdin = fopen('php://stdin', 'r');
$stdout = fopen( 'php://stdout', 'w' );
 
function execute_agi( $command )
{
GLOBAL $stdin, $stdout, $stdlog, $debug;
 
fputs( $stdout, $command . "\n" );
fflush( $stdout );
if ($debug)
fputs( $stdlog, $command . "\n" );
 
$resp = fgets( $stdin, 4096 );
 
if ($debug)
fputs( $stdlog, $resp );
 
if ( preg_match("/^([0-9]{1,3}) (.*)/", $resp, $matches) )
{
if (preg_match('/result=([-0-9a-zA-Z]*)(.*)/', $matches[2], $match))
{
$arr['code'] = $matches[1];
$arr['result'] = $match[1];
if (isset($match[3]) && $match[3])
$arr['data'] = $match[3];
return $arr;
}
else
{
if ($debug)
fputs( $stdlog, "Couldn't figure out returned string, Returning code=$matches[1] result=0\n" );
$arr['code'] = $matches[1];
$arr['result'] = 0;
return $arr;
}
}
else
{
if ($debug)
fputs( $stdlog, "Could not process string, Returning -1\n" );
$arr['code'] = -1;
$arr['result'] = -1;
return $arr;
}
}
 
 
    $vartest= 'value';
    execute_agi("SET VARIABLE ScriptResult $vartest");
?>
 

wardmundy

Nerd Uno
Joined
Oct 12, 2007
Messages
19,206
Reaction score
5,229
To send variables to the AGI script and read them...

Dialplan:
Code:
exten => 1234,1,AGI(test.agi,${SOME-VARIABLE})

test.agi:
PHP:
while ( !feof($stdin) )
{
$temp = fgets( $stdin );
 
if ($debug)
fputs( $stdlog, $temp );
 
// Strip off any new-line characters
$temp = str_replace( "\n", "", $temp );
 
$s = explode( ":", $temp );
$agivar[$s[0]] = trim( $s[1] );
if ( ( $temp == "") || ($temp == "\n") )
{
break;
}
}
 
$SOME-VARIABLE = $_SERVER["argv"][1];

For more variables, separate them with commas in the dialplan and parse the array for 2, 3, 4, etc.
 

jehowe

Guru
Joined
Nov 14, 2007
Messages
288
Reaction score
4
Bingo! It was the missing phpagi :crazy:. Thanks lgaetz, and thanks Ward for the detailed examples!
 

Members online

Forum statistics

Threads
25,825
Messages
167,861
Members
19,250
Latest member
mark-curtis
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top