Asterisk AGI: How to get or set the value of a global variable?

John Volkya picture John Volkya · Sep 30, 2011 · Viewed 16.2k times · Source

I'm using Asterisk 1.8 with PHP for AGI scripting.

EDIT:

I'm struggling with setting and obtaining the values of global variables from within an AGI PHP script. I can set channel variables but not global variables. Using PHPAGI lib.

Tried:

Set({$varname}={$value},g)
Set({$varname}=\"{$value}\",g)
Set(GLOBAL({$varname})={$value})

That does not seem to work at all, when getting the value from within the dial plan, it is empty.

Does anyone have a working example of setting and getting global variables in an AGI script?

Answer

John Volkya picture John Volkya · Sep 30, 2011

I found a workaround to make it work.

First, the global variable must not be declared in the dial plan under the [globals] section. And, it seems you cannot set a global variable from within an AGI script. However, you can set a channel variable (local to the current channel). So to set a global variable from an AGI script, you first set the value to a channel variable and when you return from the script into the dial plan, you retrieve the value of the channel variable and assign it to a global variable. Basically, it seems you can only assign global variables from within the dial plan, not from within an AGI script.

sample code:

//in dial plan

exten => _XXXX,n,AGI(myagiscript.php)
exten => _XXXX,n,Set(GLOBAL(someGlobalVariable)=${myLocalVar})


// in myagiscript.php

$agi->set_variable("myLocalVar", "value");