问题
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?
回答1:
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");
回答2:
Asterisk wiki info about AGI says different things about global variables: ... Global variables are not passed to the AGI script in this manner. You must get them using the "get variable" AGI command...
and in other part: ...GET VARIABLE: Does not work with global variables. Does not work with some variables that are generated by modules....
For setting global value you can execute:
EXEC SetGlobalVar <var_name>=<value>
For getting I thing that get_variable
should work but there was bug in Asterisk: https://issues.asterisk.org/view.php?id=7609
This bug was in Asterisk 1.2.20, what version of Asterisk do you use?
来源:https://stackoverflow.com/questions/7604257/asterisk-agi-how-to-get-or-set-the-value-of-a-global-variable