在php文件上调用exec并传递参数? [英] calling exec on a php file and passing parameters?
问题描述
我想使用 exec 调用php文件.
I am wanting to call a php file using exec.
当我调用它时,我希望能够通过(一个id)传递一个变量.
When I call it I want to be able to pass a variable through (an id).
我可以调用 echo exec("php/var/www/unity/src/emailer.php");
很好,但是当我添加 echo exec("php/var/www/unity/src/emailer.php?id=123);
exec调用失败.
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.
我该怎么做?
推荐答案
您的调用失败,因为您正在通过命令行调用使用Web样式的语法(?parameter = value
).我了解您在想什么,但这根本行不通.
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.
您将改为使用 $ argv
.参见 PHP手册.
要查看实际效果,请将此单行代码写入文件:
To see this in action, write this one-liner to a file:
<?php print_r($argv); ?>
然后从命令行使用参数调用它:
Then invoke it from the command-line with arguments:
php -f /path/to/the/file.php firstparam secondparam
您会看到 $ argv
包含脚本本身的名称,作为元素零,其后是您传入的其他任何参数.
You'll see that $argv
contains the name of the script itself as element zero, followed by whatever other parameters you passed in.
这篇关于在php文件上调用exec并传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!