简体   繁体   English

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

[英]calling exec on a php file and passing parameters?

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");我可以调用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");很好,但是当我添加类似echo exec("php /var/www/unity/src/emailer.php?id=123"); the exec call fails. exec 调用失败。

How can I do this?我怎样才能做到这一点?

Your call is failing because you're using a web-style syntax ( ?parameter=value ) with a command-line invokation.您的呼叫失败,因为您使用的是带有命令行调用的 web 样式语法 ( ?parameter=value )。 I understand what you're thinking, but it simply doesn't work.我明白你在想什么,但它根本行不通。

You'll want to use $argv instead.你会想要使用$argv来代替。 See the PHP manual .请参阅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:然后使用 arguments 从命令行调用它:

php -f /path/to/the/file.php firstparam secondparam

You'll see that $argv contains the name of the script itself as element zero, followed by whatever other parameters you passed in.您将看到$argv包含脚本本身的名称作为元素零,后跟您传入的任何其他参数。

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谢谢@hakre

try echo exec("php /var/www/unity/src/emailer.php 123");试试echo exec("php /var/www/unity/src/emailer.php 123"); in your script then read in the commandline parameters .然后在你的脚本中读入命令行参数

If you want to pass a GET parameter to it, then it's mandatory to provide a php-cgi binary 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.但这可能需要更多虚假的 CGI 环境变量。 Hencewhy it is often advisable to rewrite the called script and let it take normal commandline arguments and read them via $_SERVER["argv"] .因此,通常建议重写被调用的脚本并让它使用正常的命令行 arguments 并通过$_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 行为。)

I know this is an old thread but it helped me solve a problem so I want to offer an expanded solution.我知道这是一个旧线程,但它帮助我解决了一个问题,所以我想提供一个扩展的解决方案。 I have a php program that is normally called through the web interface and takes a long list of parameters.我有一个 php 程序,该程序通常通过 web 接口调用,并获取一长串参数。 I wanted to run it in the background with a cron job using shell_exec() and pass a long list of parameters to it.我想使用 shell_exec() 在后台使用 cron 作业运行它,并将一长串参数传递给它。 The parameter values change on each run.每次运行时参数值都会发生变化。

Here is my solution: In the calling program I pass a string of parameters that look just like the string a web call would send after the?.这是我的解决方案:在调用程序中,我传递了一串参数,这些参数看起来就像 web 调用将在?之后发送的字符串。 example: sky=blue&roses=red&sun=bright etc. In the called program I check for the existence of $argv[1] and if found I parse the string into the $_GET array.例如:sky=blue&roses=red&sun=bright 等。在被调用的程序中,我检查 $argv[1] 的存在,如果找到,我将字符串解析到 $_GET 数组中。 From that point forward the program reads in the parameters just as if they were passed from a web call.从那时起,程序读取参数,就好像它们是从 web 调用中传递的一样。

Calling program code:调用程序代码:

$pars = escapeshellarg($pars); // must use escapeshellarg()
$output = shell_exec($php_path . ' path/called_program.php ' . $pars); // $pars is the parameter string

Called program code inserted before the $_GET parameters are read:在读取 $_GET 参数之前插入的调用程序代码:

if(isset($argv[1])){ // if being called from the shell build a $_GET array from the string passed as $argv[1]
    $args = explode('&', $argv[1]); // explode the string into an array of Type=Value elements
    foreach($args as $arg){
        $TV = explode('=', $arg); // now explode each Type and Value into a 2 element array
        $_GET[$TV[0]] = $TV[1]; // set the indexes in the $_GET array
        }
    }
//------------------------
// from this point on the program processes the $_GET array normally just as if it were passed from a web call.

Works great and requires minimal changes to the called program.效果很好,并且需要对被调用程序进行最少的更改。 Hope someone finds it of value.希望有人发现它的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM