php Shell运行/执行带参数的php脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6763997/
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-26 01:10:53  来源:igfitidea点击:

Shell run/execute php script with parameters

phpshellparameterscron

提问by Patrick Lorio

I need to execute a php file with parameters through shell.

我需要通过shell执行一个带参数的php文件。

here is how I would run the php file:

这是我将如何运行 php 文件:

php -q htdocs/file.php

php -q htdocs/file.php

I need to have the parameter 'show' be passed through and

我需要传递参数“show”并且

php -q htdocs/file.php?show=show_name

php -q htdocs/file.php?show=show_name

doesn't work

不起作用

If someone could spell out to me what command to execute to get the php file to execute with set parameters, it would be much appreciated. If not, try to lead me the right direction.

如果有人可以向我说明要执行什么命令才能使用设置的参数执行 php 文件,我将不胜感激。如果没有,请尝试引导我正确的方向。

回答by schneck

test.php:

测试.php:

<?php
print_r($argv);
?>

Shell:

贝壳:

$ php -q test.php foo bar
Array
(
    [0] => test.php
    [1] => foo
    [2] => bar
)

回答by FlameStorm

If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) - just try this

如果您有网络服务器(不仅安装了 php 解释器,还安装了 LAMP/LNMP/等) - 试试这个

wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1

where:

在哪里:

  • ? -O - ? — (Letter "O", not zero!) redirect "downloaded html" to stdout
  • ? >/dev/null 2>&1 ? — redirect stdout & stderr output to nowhere
  • ? -q ? — quiet wgetrun
  • ? -t 1 ? — just 1try to connect (not like default 20)
  • ? -O -​​ ? —(字母“O”,不是零!)将“下载的 html”重定向到标准输出
  • ? >/dev/null 2>&1 ? — 将 stdout 和 stderr 输出重定向到无处
  • ? -q ? — 安静的wget运行
  • ? -t 1 ? — 仅尝试连接1次(不像默认的 20 次)

In PHP's "exec" it'll be smth like this:

在 PHP 的“exec”中,它会像这样:

function exec_local_url($url) {
  exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
    . addslashes($url) . '" >/dev/null 2>&1'
  );
}

// ...

exec_local_url("file.php?show=show_name");
exec_local_url("myframework/seo-readable/show/show_name");

So, you don't need to change your scripts to handle argc/argv, and may use $_GET as usually do.

因此,您不需要更改脚本来处理 argc/argv,并且可以像往常一样使用 $_GET。

If you want jobs runned in background - see for ex. Unix/Windows, Setup background process? from php code

如果您希望在后台运行作业 - 请参阅例如。Unix/Windows,设置后台进程?来自 php 代码

I use approach with wget in my cron jobs; hope it helps.

我在我的 cron 工作中使用 wget 方法;希望能帮助到你。

回答by Brad

You need to read command line parameters from $argcand $argv.

您需要从$argc$argv读取命令行参数。

Using a question mark is something you do in a URL and has nothing to do with executing PHP from a command line.

使用问号是您在 URL 中所做的事情,与从命令行执行 PHP 无关。

See also: http://www.sitepoint.com/php-command-line-1/

另见:http: //www.sitepoint.com/php-command-line-1/

回答by troelskn

In addition to the other answers (Which are quite correct), you can also pass arguments as environment parameters, like this:

除了其他答案(这是非常正确的),您还可以将参数作为环境参数传递,如下所示:

FOO=42 BAR=quux php test.php

They will then be available in the superglobal $_ENV.

然后它们将在 superglobal 中可用$_ENV

回答by kritya

If you are using it from a PHP file then you can use popen() and do something like this:

如果您从 PHP 文件中使用它,那么您可以使用 popen() 并执行以下操作:

$part = $show_name; //or whatever you want with spaces

$handle = popen("php -q nah.php -p=". escapeshellarg($part) . " 2>&1", "r");

This uses the escapeshellarg() function in order to wrap the $partvariable in quotes (and escape any quotes inside it), so that it can be used as a shell argument safely.

这使用escapeshellarg() 函数将$part变量括在引号中(并转义其中的任何引号),以便它可以安全地用作shell 参数。