I am wanting to call a php file using exec.
When I call it I want to be able to pass a variable through (an id).
I can call echo exec("php /var/www/unity/src/emailer.php"); fine, but the moment I add anything like echo exec("php /var/www/unity/src/emailer.php?id=123"); the exec call fails.
How can I do this?
Your call is failing because you’re using a web-style syntax (
?parameter=value) with a command-line invokation. I understand what you’re thinking, but it simply doesn’t work.You’ll want to use
$argvinstead. See the PHP manual.To see this in action, write this one-liner to a file:
Then invoke it from the command-line with arguments:
You’ll see that
$argvcontains the name of the script itself as element zero, followed by whatever other parameters you passed in.