Passing AGI variable back to Asterisk (Perl)

Ok, this is stupid, I’ve searched and tried but failed…

I simply want to pass variable from Perl AGI script to current Asterisk active channel.

In my asterisk dialplan:

[SIPTest]
exten => _.,1,AGI(testj.agi)
exten => _.,n,NoOP(${NUM})

In my testj.agi (plan text file)
#!/usr/bin/perl
use Asterisk::AGI;
my $AGI = new Asterisk::AGI;

and on the last line I tried:
SET VARIABLE NUM 123;

or

agi->set_variable(‘num’,‘123’)

or
PRINT(“SET VARIABLE NUM 123”);

nothing works, when I look at the asterisk cli, the variable “NUM” always return “”, nothing.

What am I doing wrong?

Thank you!

So now I’ve tried PHP too, no good either, some pointer please…

#!/usr/bin/php -q

<?php require('phpagi.php'); $vartest= 'value'; $agi = new AGI(); $agi->set_variable("Num", "$vartest"); ?>

I’ve downloaded the phpagi compressed file, but ONLY extracted phpagi.php and copied into /var/lib/asterisk/agi-bin, which is the same folder as the testj.php file.

That should be good enough right?

===========================

So how do I know if PHP is install and running properly? I used the code below:

#!/usr/bin/php -q

<?php require('/var/lib/asterisk/agi-bin/phpagi.php'); $agi = new AGI(); noop("no match"); function noop($message){ echo ("NOOP " . $message . "\n"); } ?>

Which according to the blog, it suppose to return “no match” on the Asterisk CLI, but I don’t see it, I turned on AGI debug I only get “script completed”:

-- Executing [1@test1:1] AGI("SIP/TEST-0000040c", "testj.php") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/testj.php

<SIP/TEST-0000040c>AGI Tx >> agi_request: testj.php
<SIP/TEST-0000040c>AGI Tx >> agi_channel: SIP/TEST-0000040c
<SIP/TEST-0000040c>AGI Tx >> agi_language: en
<SIP/TEST-0000040c>AGI Tx >> agi_type: SIP
<SIP/TEST-0000040c>AGI Tx >> agi_uniqueid: 1408402494.1036
<SIP/TEST-0000040c>AGI Tx >> agi_version: 11.3.0
<SIP/TEST-0000040c>AGI Tx >> agi_callerid: TEST
<SIP/TEST-0000040c>AGI Tx >> agi_calleridname: unknown
<SIP/TEST-0000040c>AGI Tx >> agi_callingpres: 0
<SIP/TEST-0000040c>AGI Tx >> agi_callingani2: 0
<SIP/TEST-0000040c>AGI Tx >> agi_callington: 0
<SIP/TEST-0000040c>AGI Tx >> agi_callingtns: 0
<SIP/TEST-0000040c>AGI Tx >> agi_dnid: 1
<SIP/TEST-0000040c>AGI Tx >> agi_rdnis: unknown
<SIP/TEST-0000040c>AGI Tx >> agi_context: test
<SIP/TEST-0000040c>AGI Tx >> agi_extension: 1
<SIP/TEST-0000040c>AGI Tx >> agi_priority: 1
<SIP/TEST-0000040c>AGI Tx >> agi_enhanced: 0.0
<SIP/TEST-0000040c>AGI Tx >> agi_accountcode: 0000
<SIP/TEST-0000040c>AGI Tx >> agi_threadid: 140330817902336
<SIP/TEST-0000040c>AGI Tx >>
– <SIP/TEST-0000040c>AGI Script testj.php completed, returning 0
– Executing [1@test:2] NoOp(“SIP/TEST-0000040c”, “”) in new stack

========================================

I also tried from this blog:
Example of agiscript.php:

#!/usr/bin/php -q

<? require ‘phpagi.php'; $agi = new AGI(); //DB connect $db = ‘xxx'; $dbuser = ‘xxxxxx'; $dbpass = ‘xxxxxxxxxl'; $dbhost = ‘x.x.x.x'; mysql_connect($dbhost,$dbuser,$dbpass) or die(“Unable to connect to MySQL”); $select_db=mysql_select_db(“$db”); //End $mobilen=$argv[1]; $row=mysql_query(“SELECT * FROM dbeurocom WHERE mobilen like ‘%$mobilen%’ “); $get_data=mysql_fetch_array($row); if (mysql_num_rows($row)!==1){ $pora4ka=”unavailable”; $agi->set_variable(“pora4ka”, $pora4ka); }else{ $pora4ka=$get_data['pora4ka']; $pora4ka = ereg_replace(‘[^0-9]‘, ”, $pora4ka); $agi->exec(‘Set’,”pora4ka”, $pora4ka); $agi->set_variable(“pora4ka”, $pora4ka); } ?>

Useful CLI command:

elastix*CLI> agi set debug on

After this output is like the following:

– Launched AGI Script /var/lib/asterisk/agi-bin/agiscript.php
<SIP/2510-00015d>AGI Tx >> agi_request: agiscript.php
<SIP/2510-00015d>AGI Tx >> agi_channel: SIP/2510-00015dfd
<SIP/2510-00015d>AGI Tx >> agi_language: en
<SIP/2510-00015d>AGI Tx >> agi_type: SIP
<SIP/2510-00015d>AGI Tx >> agi_uniqueid: 1381148670.103392
<SIP/2510-00015d>AGI Tx >> agi_version: 11.4.0
<SIP/2510-00015d>AGI Tx >> agi_callerid: 2510
<SIP/2510-00015d>AGI Tx >> agi_calleridname: device
<SIP/2510-00015d>AGI Tx >> agi_callingpres: 0
<SIP/2510-00015d>AGI Tx >> agi_callingani2: 0
<SIP/2510-00015d>AGI Tx >> agi_callington: 0
<SIP/2510-00015d>AGI Tx >> agi_callingtns: 0
<SIP/2510-00015d>AGI Tx >> agi_dnid: 0888551881
<SIP/2510-00015d>AGI Tx >> agi_rdnis: unknown
<SIP/2510-00015d>AGI Tx >> agi_context: custom-out
<SIP/2510-00015d>AGI Tx >> agi_extension: 888551881
<SIP/2510-00015d>AGI Tx >> agi_priority: 1
<SIP/2510-00015d>AGI Tx >> agi_enhanced: 0.0
<SIP/2510-00015d>AGI Tx >> agi_accountcode:
<SIP/2510-00015d>AGI Tx >> agi_threadid: 47954860501312
<SIP/2510-00015d>AGI Tx >> agi_arg_1: 88551881
<SIP/2510-00015d>AGI Tx >>
<SIP/2510-00015d>AGI Rx << EXEC Set pora4ka
— AGI Script Executing Application: (Set) Options: (pora4ka)
<SIP/2510-00015d>AGI Tx >> 200 result=0
<SIP/2510-00015d>AGI Rx << SET VARIABLE pora4ka “4061”
<SIP/2510-00015d>AGI Tx >> 200 result=1
— <SIP/2510-00015d>AGI Script agiscript.php completed, returning 0

And all I am getting is AGI Tx, I don’'t get AGI Rx, why is that? It seems aterisk is not executing the code or something?

Solved… I am missing phagi-asmanager.php… Once put that in, it worked fine!