[asterisk-users] System Command not executing php

Steve Edwards asterisk.org at sedwards.com
Wed Aug 24 17:48:37 CDT 2011


On Wed, 24 Aug 2011, J Gao wrote:

> I admit that using AGI() may not correct here. This is a work around 
> about passing multiple args to php script. (I had this trouble before.) 
> I would like to know what is the correct way to call the php script 
> through SYSTEM().

(Please keep in mind I'm not a PHP weenie so my experiences may be colored 
by my C background.)

Depending on how your host is set up and the contents of your script, it 
may be as simple as:

         exten = *,n,system(example.php)

This assumes your script starts with a 'shebang' like:

 	#!/usr/bin/php -f

and that the path to example.php is enumerated in the PATH environment 
variable of the Asterisk process.

If these conditions are not met, you can specify your way around them in 
the parameters (arguments sound so argumentative) passed to the system() 
application.

The process executing Asterisk must also have execute permission to every 
directory in the path to the script and both read and execute permissions 
to the script. Using '777' is insecure and the mark of a newb.

You can pass multiple parameters on the command line like:

         exten = *,n,system(echo ${EPOCH} ${CHANNEL} >/tmp/foo)

You can confirm its execution with:

 	cat /tmp/foo

(When I wrote this example, I used the EXTEN channel variable instead of 
EPOCH. Extra points for the first reader to explain why that was not a 
good example. Anyone, anyone?)

So the problem is not passing multiple parameters, but accessing them in 
your program.

In most programming languages, the program initialization code creates and 
populates an array containing either the parameters or pointers to the 
parameters.

In PHP, the array is named $argv. In C, it is whatever the programmer 
named the second parameter to the main() function. I think most C 
programmers name this array of char pointers argv because everybody 
expects that it have this name.

In any case, the 'zeroth' element contains the path to the program which 
may be useful if you have multiple links to the program because you want 
the program to behave differently depending on which link was used to 
execute the program. Probably not, but now you know.

Within your script you can access the command line parameters with code 
like:

#!/usr/bin/php -f
<?

         echo "The zeroth parameter is \"$argv[0]\"\n";
         echo "The first parameter is \"$argv[1]\"\n";
         echo "The second parameter is \"$argv[2]\"\n";

         exit(0);

?>

If you were to execute this script with a dialplan like:

         exten = *,n,system(echo.php ${EPOCH} ${CHANNEL} >/tmp/foo)

you would find that /tmp/foo contained:

The zeroth parameter is "./echo.php"
The first parameter is "1314224887" 
The second parameter is "SIP/841-line-1-b7961340"

Personally (leaping up on my soapbox), I find specifying a bunch of 
anonymous parameters difficult to maintain. I prefer to use the getopt() 
function in PHP (or getopt_long() function in C) and define a 
'self-documenting' command line. (I also do this for my AGIs.)

For example, I would define '--channel' and '--epoch' options so that I 
could execute the above example like:

         exten = *,n,system(echo.php --epoch=${EPOCH} --channel=${CHANNEL})

or

         exten = *,n,system(echo.php --channel=${CHANNEL} --epoch=${EPOCH})

Aside from allowing you to specify the parameters in any order (I'm big on 
'alphabetization'), it is much easier to maintain when you pass several 
parameters.

-- 
Thanks in advance,
-------------------------------------------------------------------------
Steve Edwards       sedwards at sedwards.com      Voice: +1-760-468-3867 PST
Newline                                              Fax: +1-760-731-3000



More information about the asterisk-users mailing list