Can you call a PHP script from a context?

I’ve mostly got things how I want them to work. But, I’m trying to figure out how I can include within a context something like: exten => s,n,PHP(execute_this_script.php)

Essentially when someone presses 1 or 2 at my phone menu, I want a php script to run and modify the mysql database. Do I have to write an AGI script? I haben’t a clue how to do so if that’s case.

you are 95% there in your understanding:

exten => s,n,PHP(execute_this_script.php) becomes:

exten => s,n,AGI(execute_this_script.php)

your script is usually located in /var/lib/asterisk/agi-bin

So, the script, is it script.agi located in that directory? Or, script.php located in that directory? And, are there any special syntax requirements for the script; or can I just write a standard .php script?

using your example, put “execute_this_script.php” in that directory. standard php works fine.

I’d just like to say that if you are using a normal PHP script that will NOT be using the AGI functions, then don’t run it using the AGI command. Instead use System().

The reason why is that when using AGI you create unnecessary overhead (albeit that it is small). If you are running this script frequently then the overhead adds up, and it can effect performance on systems with heavy load.

So just to recap, if your PHP script uses AGI commands, then use it via:

If it DOESN’T use AGI commands, then use it via:

good point. i assumed [always a bad idea] that wanting to update the database from the dial plan meant that the OP wanted to update it with some call related information so would need to access AGI and maybe even put data back into the dial plan [such as a success/fail of the database update].

Alright, so here’s the latest.

I tried exten => System(/usr/share/asterisk/agi-bin/confirm.php)

While tailing the /var/log/asterisk/messages log file, I do see this:

System(“SIP/voipvoip-014875f0”, “/usr/share/asterisk/agi-bin/confirm.php”) in new stack

However, I know that the script isn’t actually being executed. When I execute the script from the command line:
php confirm.php, it does exactly what I want it to do. It updates a column in one of my mysql database tables with the correct information. But, like I said, when it is executed via pressing 1 at the phone menu, nothing changes.

Any ideas?

Ya, look what you just typed.

If you have to run it on the command line by saying:

Then your system statement also has to have:

All System() does is execute exactly what you type inside it on the command line. If you want to run your script without typing php first, then you need to use the shebang syntax to your PHP interpreter and set the executable bit on your program.

To do the shebang, you need to add:

To the top of your program. Then run:

Now if you just type:

On the command line the program will run (and your System command will also work).

Yep, I actually realized that and fixed it. Then, came back here to mention that :wink: Thanks.


This has less to do with PHP but, has to do with the System command, so, if I put this into the wrong topic, i apologize.

I am trying to run a command from context, that, runs as is (as seen from the CLI) but, does not actually execute when invoked.

I added this to a custom context in extensions_custom.conf

exten => s,n(send),System(/bin/echo -e '@CALL${CALLERID(name)}~${CALLERID(num)}'| /bin/nc -w 1 172.20100.8 10629)
basically, I want to dump the callerId through NC (netcat) to another host.

When the call is placed,the context is invoked, i see this in my CLI

-- Executing [s@custom-callerID:9] System("Local/7890@from-internal-c1ff;2", "/bin/echo -e '@CALL410~410'| /bin/nc -w 1 172.20100.8 10629") in new stack -- Auto fallthrough, channel 'Local/7890@from-internal-c1ff;2' status is 'UNKNOWN'

I suspect that auto fallthrough line is indicating that my command could not be run.
From the linux shell, i can successfully run the command

I wrote a TCP listener on the target machine that will dump the results of the request to the screen. So, when asterisk attempts to run the command, i see nothing, but, when i run the same command from the shell, i see the result (the contents of the quotes).

I ran chmod +x on my nc file, just in case this was a permissions issue.

Any help would be greatly appreciated.

Thank you

ok, forget all that.
i realized after i posted that i forgot a . in my IP address.
it runs

thank you though