# Get\_variable in AGI

**URL:** <https://community.asterisk.org/t/get-variable-in-agi/87172>\
**Category:** Asterisk APIs\
**Created:** [January 18, 2021, 7:46am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172 "2021-01-18T07:46:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simplesunny](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/simplesunny/32/3721_2.png) [@Simplesunny](https://community.asterisk.org/u/Simplesunny)\
**Post date:** [January 18, 2021, 7:46am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/1 "2021-01-18T07:46:17Z")

</div>

Hi,

I am using Asterisk certified/16.8-cert3

and in AGI I am setting the static value and need to retrieve or get the same value

$fastagi-\>set\_variable(“plan\_id”, 123);  
$plan\_id = $fastagi-\>get\_variable(‘plan\_id’);  
$fastagi-\>verbose(“MYRESULT-\>”.$plan\_id);

Below is my full debug log line where it shows that I am getting the value “123” but not able to use that in the same AGI, just wanted to know the functionality for get\_variable

[Jan 18 07:10:43] DEBUG[853153][C-00000002]: pbx\_variables.c:375 ast\_str\_retrieve\_variable: Result of ‘plan\_id’ is ‘123’

---

<div class="post-metadata">

**Author:** ![david551](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@david551](https://community.asterisk.org/u/david551)\
**Post date:** [January 18, 2021, 11:02am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/2 "2021-01-18T11:02:02Z")

</div>

Why are you using a certified version, rather than the latest, when you aren’t getting paid support from Sangoma?

Which AGI class library are you using?

You don’t appear to have enabled AGI debugging.

---

<div class="post-metadata">

**Author:** ![Simplesunny](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/simplesunny/32/3721_2.png) [@Simplesunny](https://community.asterisk.org/u/Simplesunny)\
**Post date:** [January 18, 2021, 11:33am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/3 "2021-01-18T11:33:07Z")

</div>

> [@Simplesunny](#):
>
> st wanted to know the functionality for get\_variable

Hi @david551

I will upgrade to Asterisk 18.1  
and currently, we are using PHP-AGI 2.2

Below is the AGI Debug enabled logs where I am getting the information that get\_variable has the value “123” but unable to use that in my agi file

\<PJSIP/1002202-0000000c\>AGI Tx \>\> 200 result=1  
\<PJSIP/1002202-0000000c\>AGI Rx \<\< GET VARIABLE plan\_id  
\<PJSIP/1002202-0000000c\>AGI Tx \>\> 200 result=1 (123)

---

<div class="post-metadata">

**Author:** ![david551](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@david551](https://community.asterisk.org/u/david551)\
**Post date:** [January 18, 2021, 11:47am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/4 "2021-01-18T11:47:56Z")

</div>

That suggests the problem lies with the library, rather than Asterisk.

---

<div class="post-metadata">

**Author:** ![ambiorixg12](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/ambiorixg12/32/1887_2.png) [@ambiorixg12](https://community.asterisk.org/u/ambiorixg12)\
**Post date:** [January 18, 2021, 2:07pm UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/5 "2021-01-18T14:07:47Z")

</div>

declare the variable on the php enviroment

$myvar=123;

looks this method set\_variable() doesnt not create the variable on php side.

---

<div class="post-metadata">

**Author:** ![sedwards](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sedwards](https://community.asterisk.org/u/sedwards)\
**Post date:** [January 18, 2021, 4:04pm UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/6 "2021-01-18T16:04:49Z")

</div>

Please post code snippets wrapped in preformatted text tags so the forum software does not eat important characters like ‘$’ or change quotes to funky quotes.

> [@Simplesunny](#):
>
> \<PJSIP/1002202-0000000c\>AGI Tx \>\> 200 result=1  
> \<PJSIP/1002202-0000000c\>AGI Rx \<\< GET VARIABLE plan\_id  
> \<PJSIP/1002202-0000000c\>AGI Tx \>\> 200 result=1 (123)

Your console log snippet shows you retrieved the channel variable, but not what happened when you execute ‘verbose()’. It probably looks like:

```auto
<SIP/poly-77a1-00000007>AGI Rx << VERBOSE "MYRESULT->Array" 1
 example.php: MYRESULT->Array
<SIP/poly-77a1-00000007>AGI Tx >> 200 result=1

```

meaning ‘$plan\_id’ is an array, not a scalar.

The ‘get\_variable()’ method, returns an array. The array consists of 3 members: code, data, and result. You can access the members like:

```auto
        $agi->verbose('code->'.$plan_id['code']);
        $agi->verbose('data->'.$plan_id['data']);
        $agi->verbose('result->'.$plan_id['result']);

```

Which will result in a console log like:

```auto
<SIP/poly-77a1-00000008>AGI Rx << VERBOSE "code->200" 1
 example.php: code->200
<SIP/poly-77a1-00000008>AGI Tx >> 200 result=1
<SIP/poly-77a1-00000008>AGI Rx << VERBOSE "data->123" 1
 example.php: data->123
<SIP/poly-77a1-00000008>AGI Tx >> 200 result=1
<SIP/poly-77a1-00000008>AGI Rx << VERBOSE "result->1" 1
 example.php: result->1

```

---

<div class="post-metadata">

**Author:** ![ambiorixg12](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/ambiorixg12/32/1887_2.png) [@ambiorixg12](https://community.asterisk.org/u/ambiorixg12)\
**Post date:** [January 18, 2021, 10:57pm UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/7 "2021-01-18T22:57:06Z")

</div>

> [@sedwards](#):
>
> The ‘get-variable()’ method, returns an array. The array consists of 3 members: code, data, and result. You can access the members like:
> 
> ```auto
> 
> ```

If return an arrary then is just use the key and value holding the data

---

<div class="post-metadata">

**Author:** ![Simplesunny](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/simplesunny/32/3721_2.png) [@Simplesunny](https://community.asterisk.org/u/Simplesunny)\
**Post date:** [January 21, 2021, 7:29am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/8 "2021-01-21T07:29:29Z")

</div>

Hi @sedwards, Thanks but it is not working in my case as I am using fastagi on Ast 18.1.1 with PHP-AGI 2.2

```auto
$fastagi->set_variable("plan_id", "123654");
$plan_id = $fastagi->get_variable("plan_id");

 $fastagi->verbose(print_r($plan_id, TRUE));
 $fastagi->verbose(print_r(${plan_id}, TRUE));

$fastagi->verbose('code->'.$plan_id['code']);
$fastagi->verbose('data->'.$plan_id['data']);
$fastagi->verbose('result->'.$plan_id['result']);

================LOGS=======================

<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << GET VARIABLE plan_id
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1 (123654)
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "Array" 1
 agi://127.0.0.1:4761/check.php,18000000000: Array
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "(" 1
 agi://127.0.0.1:4761/check.php,18000000000: (
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE " [code] => 200" 1
 agi://127.0.0.1:4761/check.php,18000000000: [code] => 200
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE " [result] => 1" 1
 agi://127.0.0.1:4761/check.php,18000000000: [result] => 1
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE " [data] => " 1
 agi://127.0.0.1:4761/check.php,18000000000: [data] =>
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE ")" 1
 agi://127.0.0.1:4761/check.php,18000000000: )
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "" 1
 agi://127.0.0.1:4761/check.php,18000000000:
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "Array" 1
 agi://127.0.0.1:4761/check.php,18000000000: Array
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "(" 1
 agi://127.0.0.1:4761/check.php,18000000000: (
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE " [code] => 200" 1
 agi://127.0.0.1:4761/check.php,18000000000: [code] => 200
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE " [result] => 1" 1
 agi://127.0.0.1:4761/check.php,18000000000: [result] => 1
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE " [data] => " 1
 agi://127.0.0.1:4761/check.php,18000000000: [data] =>
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE ")" 1
 agi://127.0.0.1:4761/check.php,18000000000: )
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "" 1
 agi://127.0.0.1:4761/check.php,18000000000:
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "code->200" 1
 agi://127.0.0.1:4761/check.php,18000000000: code->200
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "data->" 1
 agi://127.0.0.1:4761/check.php,18000000000: data->
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << VERBOSE "result->1" 1
 agi://127.0.0.1:4761/check.php,18000000000: result->1
<PJSIP/1002202-00000015>AGI Tx >> 200 result=1
<PJSIP/1002202-00000015>AGI Rx << SET VARIABLE plan_id "123654"

```

Which version of AGI you are using?

---

<div class="post-metadata">

**Author:** ![sedwards](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sedwards](https://community.asterisk.org/u/sedwards)\
**Post date:** [January 21, 2021, 7:58am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/9 "2021-01-21T07:58:45Z")

</div>

At this stage of the game, I don’t think fastagi is your issue. Try just ‘plain agi’ to confirm.

The AGI protocol is a line oriented request/response kind of animal – you issue 1 request, you read 1 response, lather, rinse, repeat.

I suspect using print\_r() is your issue. This is going to output a mulit-line description of $plan\_id, thus, breaking the AGI protocol. Try it without the 2 verbose(print\_r()) statements.

> [@Simplesunny](#):
>
> Which version of AGI you are using?

Which version of _phpagi_ am I using? 2.20.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/asterisk/original/2X/4/475d0c889911cfb84ac4881ad6df8b523e3f9105.png) [@system](https://community.asterisk.org/u/system)\
**Post date:** [February 20, 2021, 7:58am UTC](https://community.asterisk.org/t/get-variable-in-agi/87172/10 "2021-02-20T07:58:51Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
