在 php 文件上调用 exec 并传递参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6273791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:59  来源:igfitidea点击:

calling exec on a php file and passing parameters?

phpvariablesexec

提问by Hailwood

I am wanting to call a php file using exec.

我想使用exec调用一个 php 文件。

When I call it I want to be able to pass a variable through (an id).

当我调用它时,我希望能够通过(一个 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.

我可以正常调用echo exec("php /var/www/unity/src/emailer.php");,但是当我添加echo exec("php /var/www/unity/src/emailer.php?id=123");exec 调用失败时。

How can I do this?

我怎样才能做到这一点?

回答by Chris Allen Lane

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.

您的调用失败,因为您使用的是?parameter=value带有命令行调用的网络样式语法 ( )。我明白你在想什么,但这根本行不通。

You'll want to use $argvinstead. See the PHP manual.

你会想要使用$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

You'll see that $argvcontains the name of the script itself as element zero, followed by whatever other parameters you passed in.

您将看到$argv包含脚本本身的名称作为元素零,后跟您传入的任何其他参数。

回答by hakre

try echo exec("php /var/www/unity/src/emailer.php 123");in your script then read in the commandline parameters.

试试echo exec("php /var/www/unity/src/emailer.php 123");你的脚本,然后读入命令行参数

回答by tony gil

this adapted script shows 2 ways of passing parameters to a php script from a php exec command: CALLING SCRIPT

这个改编的脚本显示了从 php exec 命令将参数传递给 php 脚本的 2 种方法:CALLING SCRIPT

<?php 
$fileName = '/var/www/ztest/helloworld.php 12';
$options = 'target=13';
exec ("/usr/bin/php -f {$fileName} {$options} > /var/www/ztest/log01.txt 2>&1 &");

echo "ended the calling script"; 
?>

CALLED SCRIPT

调用脚本

<?php
echo "argv params: ";
print_r($argv); 
if ($argv[1]) {echo "got the size right, wilbur!  argv element 1: ".$argv[1];}
?> 

dont forget to verify execution permissions and to create a log01.txt file with write permissions (your apache user will usually be www-data).

不要忘记验证执行权限并创建一个具有写入权限的 log01.txt 文件(您的 apache 用户通常是 www-data)。

RESULT

结果

argv params: Array

argv 参数:数组

(

(

[0] => /var/www/ztest/helloworld.php

[1] => 12

[2] => target=13

)

)

got the size right, wilburargv element 1: 12

尺寸合适, wilburargv 元素 1: 12

choose whatever solution you prefer for passing your parameters, all you need to do is access the argv array and retrieve them in the order that they are passed (file name is the 0 element).

选择您喜欢传递参数的任何解决方案,您需要做的就是访问 argv 数组并按照传递的顺序检索它们(文件名是 0 元素)。

tks @hakre

tks @hakre

回答by mario

If you want to pass a GET parameter to it, then it's mandatory to provide a php-cgibinary for invocation:

如果你想向它传递一个 GET 参数,那么必须提供一个php-cgi二进制文件来调用:

exec("QUERY_STRING=id=123 php-cgi /var/www/emailer.php");

But this might require more fake CGI environment variables. Hencewhy it is often advisable to rewrite the called script and let it take normal commandline arguments and read them via $_SERVER["argv"].

但这可能需要更多虚假的 CGI 环境变量。因此,为什么通常建议重写被调用的脚本并让它接受普通的命令行参数并通过$_SERVER["argv"].

(You could likewise just fake the php-cgi behaviour with a normal php interpreter and above example by adding parse_str($_SERVER["QUERY_STRING"], $_GET);on top of your script.)

(你同样可以通过parse_str($_SERVER["QUERY_STRING"], $_GET);在你的脚本之上添加一个普通的 php 解释器和上面的例子来伪造 php-cgi 行为。)