3

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?

P Bear
  • 92
  • 1
  • 11
J. Volkya
  • 993
  • 3
  • 14
  • 33
  • I have no experience with Asterisk but with php you can access global variables from [`$GLOBALS`](http://us2.php.net/manual/en/reserved.variables.globals.php) – Mike B Sep 29 '11 at 22:58
  • No, I'm talking about an Asterisk dial plan global variable, which can be set via an external program or script using the Asterisk Gateway Interface (AGI). – J. Volkya Sep 29 '11 at 23:23

2 Answers2

2

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");
J. Volkya
  • 993
  • 3
  • 14
  • 33
0

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?

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114