本文介绍了Asterisk AGI:如何获取或设置全局变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Asterisk 1.8 和 PHP 来编写 AGI 脚本.

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

我正在努力从 AGI PHP 脚本中设置和获取全局变量的值.我可以设置通道变量,但不能设置全局变量.使用 PHPAGI 库.

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.

尝试过:

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.

有人有在 AGI 脚本中设置和获取全局变量的工作示例吗?

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

推荐答案

我找到了一种解决方法来使其正常工作.

I found a workaround to make it work.

首先,不能在拨号方案的 [globals] 部分下声明全局变量.而且,您似乎无法从 AGI 脚本中设置全局变量.但是,您可以设置通道变量(当前通道的本地变量).因此,要从 AGI 脚本设置全局变量,首先将值设置为通道变量,然后从脚本返回拨号计划时,检索通道变量的值并将其分配给全局变量.基本上,您似乎只能从拨号计划内分配全局变量,而不能从 AGI 脚本内分配.

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.

示例代码:

//in dial plan

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


// in myagiscript.php

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

这篇关于Asterisk AGI:如何获取或设置全局变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 18:48