Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php文件上调用exec并传递参数?_Php_Variables_Exec - Fatal编程技术网

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

在php文件上调用exec并传递参数?,php,variables,exec,Php,Variables,Exec,我想使用调用一个php文件 当我调用它时,我希望能够通过(一个id)传递一个变量 我可以调用echo exec(“php/var/www/unity/src/emailer.php”)很好,但是当我添加类似echo exec(“php/var/www/unity/src/emailer.php?id=123”)的内容时exec调用失败 我该怎么做呢?试试echo exec(“php/var/www/unity/src/emailer.php 123”)代码,然后读入 如果要向其传递GET参数,则

我想使用调用一个php文件

当我调用它时,我希望能够通过(一个id)传递一个变量

我可以调用echo exec(“php/var/www/unity/src/emailer.php”)很好,但是当我添加类似echo exec(“php/var/www/unity/src/emailer.php?id=123”)的内容时exec调用失败


我该怎么做呢?

试试
echo exec(“php/var/www/unity/src/emailer.php 123”)代码,然后读入

如果要向其传递GET参数,则必须为调用提供php cgi二进制文件:

exec("QUERY_STRING=id=123 php-cgi /var/www/emailer.php");
但这可能需要更多伪造的CGI环境变量。因此,通常建议重写被调用的脚本,并让它接受正常的命令行参数,然后通过
$\u SERVER[“argv”]
读取它们


(同样,您可以使用普通的php解释器和上面的示例,通过在脚本顶部添加
parse_str($\u SERVER[“QUERY_STRING”],$\u GET);
)来伪造php cgi行为。)

您的调用失败,因为您在命令行调用中使用了web风格的语法(
?parameter=value
)。我理解你的想法,但这根本不起作用

您将希望改用
$argv
。看

要查看此操作,请将此一行程序写入文件:

<?php print_r($argv); ?>

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

此改编脚本显示了从php exec命令向php脚本传递参数的两种方法: 调用脚本

<?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"; 
?>
)

尺寸正确,威尔伯拉格夫元素1:12

选择您希望传递参数的任何解决方案,您只需访问argv数组并按传递顺序检索它们(文件名为0元素)


tks@hakre

我知道这是一个旧线程,但它帮助我解决了一个问题,所以我想提供一个扩展的解决方案。我有一个php程序,通常通过web界面调用,并获取一长串参数。我想在后台使用shell_exec()在cron作业中运行它,并将一长串参数传递给它。参数值在每次运行时都会更改

这是我的解决方案:在调用程序中,我传递一个参数字符串,它看起来就像web调用在?之后发送的字符串?。示例:sky=blue&roses=red&sun=bright等。在调用的程序中,我检查$argv[1]是否存在,如果找到,我将字符串解析到$\u GET数组中。从这一点开始,程序读入参数,就像它们是从web调用传递的一样

调用程序代码:

$pars = escapeshellarg($pars); // must use escapeshellarg()
$output = shell_exec($php_path . ' path/called_program.php ' . $pars); // $pars is the parameter string
在读取$\u 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.

非常有效,只需对调用的程序进行最小的更改。希望有人觉得它有价值。

这真是一个深奥的问题。如果您从命令行执行php--help,它会这样说:“-f Parse and execute.”但是由于php无论如何都会这样做(不调用-f选项),我想这是一个好问题:它是做什么的?:)也许这是给强迫症患者的,他们必须通过旗帜;)在我的例子中,为了能够在php exec
php-q./yii.php“migrate/up--interactive=0--migrationPath=@vendor/pheme/yii2 settings/migrations
中执行和传递参数,我必须使用本文提到的完整语法,在被调用的脚本中,包括这条
$argv
来自何处的行?很明显,这是一个您没有在任何地方声明的变量???@ekashking它似乎是一个PHP保留变量,请检查以下内容:
[0] => /var/www/ztest/helloworld.php

[1] => 12

[2] => target=13
$pars = escapeshellarg($pars); // must use escapeshellarg()
$output = shell_exec($php_path . ' path/called_program.php ' . $pars); // $pars is the parameter string
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.