AGI Scripts arguments

Hi ! I would like to get the dialed number in my php scripts, so i write the extension like this :

exten=> _X.,1,Set(CallerID=${CALLERID(dnid)})
exten=> _X.,2,agi(FirebasePush.php, ${CallerID})
exten => _X.,n,Dial(PJSIP/${EXTEN},20)

The problem is that i can’t take the argument with argv in php. What is wrong with this extension ? Do i have to include some file in my php script ?

Hi, @RobMat.

In my opinion you don’t need the CallerID variable, since you only use it once.

I’m not sure, but this probably don’t work because the name callerid is already used by Asterisk.

You can do that:

exten=> _X.,2,agi(FirebasePush.php,${CALLERID(dnid)})

Or try another variable name, for example CallerDnid. And if you allow me another tip, don’t repeat yourself. Try this:

exten => _X.,1,Set(CallerDnid=${CALLERID(dnid)})
same => n,Agi(FirebasePush.php,${CallerDnid})
same => n,Dial(PJSIP/${EXTEN},20)

Thanks for your reply ! I will test that soon. Is the variable going to be taken as an argument of the script ? Can i take it like this : argv[1] ?

If you pass the value in the Agi(script_name.agi,var1,var2,...), yes you can read it in the argv.

But I recommend you to use some library, to help you with the communication (read variables, executing commands, etc) with Asterisk.

For example, in the AGI script you can read variables from the channel, you don’t need to pass all the values by parameter to the AGI.

Check in this page some of the PHP’s libraries:

I’ve used the PHPAGI.

  1. Channel variable names are case sensitive.
  2. Neither CallerID nor CALLERID are channel variables.
  3. The ‘dnid’ is passed by Asterisk to your AGI as part of the ‘AGI environment’ via STDIN.
  4. You will probably want to use the ‘phpagi’ library at some point.
  5. If you enable AGI debugging (‘agi set debug on’) you can observe the interaction between Asterisk and your AGI. Very ‘clueful.’

Arguments are also available as AGI environment variables in the form ‘agi_arg_x’ where x is the position in the argument list passed to agi().

Personally, I don’t like positional arguments. I prefer passing arguments as ‘long options’ and parsing argv using ‘getopt_long().’ For example, I would pass arguments like:


This way, I never have to remember “what’s the 3rd argument used for?” I also pass the arguments in alphabetic order for consistency.

Thank you for your answers ! I will look that.
I only need one variable, that’s the “number” to call (it’s a simple push notification script). I follow @aabreu’s context but i have some trouble with it.
When a user connect on an endpoint with this extension [test], the script is executed in a loop WITHOUT calling someone. I tried to restart and more but i can’t find what is the problem. It’s a Dialplan, so i really don’t know what to do.

But my script do not have loop, i can execute it manually like this “php name.php arg”, and it stop and execute normally.

Please reply with a console log, with AGI debugging enabled, and debugging and verbose bumped up. While in the Asterisk CLI, enter:

agi set debug on
core set debug 3
core set verbose 3
dialplan show test (or whatever context name you used in your dialplan)

And then make a call that executes this context.

Please remember to cut and paste the text enclosed in ‘preformatted text’ tags (’</>’ in the Reply window icon bar. No retyping and no pictures :slight_smile:

If your AGI script is reasonably short (a couple dozen lines), please include it (in preformatted text tags) as well.

Feel free to sanitize any values you consider secret like telephone numbers, public IP addresses, passwords, etc.

First, verify you have a value same=>n,Noop( *** ${CallerID} ***)

and I don’t know if that space matter, but you have an space, also you can use system or shell command to run your PHP script but using them you won’t have the AGI log

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