php 如何将参数从bash传递到php脚本?

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

How to pass parameters from bash to php script?

phpbashshellparametersparameter-passing

提问by user420574

I have done a a bash script which run php script. It works fine without parameters but when I add parameters (id and url), there are some errors:

我已经完成了运行 php 脚本的 bash 脚本。它在没有参数的情况下工作正常,但是当我添加参数(id 和 url)时,会出现一些错误:

PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf                                                                                        .d/mcrypt.ini on line 1 in Unknown on line 0
Could not open input file: /var/www/dev/dbinsert/script/automatisation.php?                                                                                        id=1

I run php script from the bash like this:

我像这样从 bash 运行 php 脚本:

php /var/www/dev/dbinsert/script/automatisation.php?id=19&url=http://bkjbezjnkelnkz.com

回答by Tino Didriksen

Call it as:

称之为:

php /path/to/script/script.php -- 'id=19&url=http://bkjbezjnkelnkz.com'

Also, modify your PHP script to use parse_str():

此外,修改您的 PHP 脚本以使用parse_str()

parse_str($argv[1]);

If the index $_SERVER['REMOTE_ADDR']isn't set.

如果$_SERVER['REMOTE_ADDR']未设置索引。



More advanced handling may need getopt(), but parse_str()is a quick'n'dirty way to get it working.

更高级的处理可能需要getopt(),但parse_str()是让它工作的一种快速而简单的方法。

回答by Patrick Cavanaugh

You can't pass GET query parameters to the PHP command line interface. Either pass the arguments as standard command line arguments and use the $argcand $argvglobalsto read them, or (if you must use GET/POST parameters) call the script through curl/wget and pass the parameters that way – assuming you have the script accessible through a local web server.

您不能将 GET 查询参数传递给 PHP 命令行界面。无论是传递参数为标准的命令行参数,并使用$argc$argv全局读他们,或者(如果必须使用GET / POST参数)调用通过卷曲/ wget的脚本并传递参数的方式-假设你有脚本访问通过本地网络服务器。

This is how you can pass arguments to be read by $argcand $argv(the -- indicates that all subsequent arguments should go to the script and not to the PHP interpreter binary):

这是传递参数以供$argc和读取的方式$argv( -- 表示所有后续参数应转到脚本而不是 PHP 解释器二进制文件):

php myfile.php -- argument1 argument2

php myfile.php -- argument1 argument2

回答by Bastion

-- Option 1: php-cgi --

-- 选项 1: php-cgi --

Use 'php-cgi' in place of 'php' to run your script. This is the simplest way as you won't need to specially modify your php code to work with it:

使用 'php-cgi' 代替 'php' 来运行您的脚本。这是最简单的方法,因为您无需专门修改 php 代码即可使用它:

php-cgi -f /my/script/file.php id=19 myvar=xyz

-- Option 2: if you have a web server --

-- 选项 2:如果您有网络服务器 --

If the php file is on a web server you can use 'wget' on the command line:

如果 php 文件在 Web 服务器上,您可以在命令行上使用“wget”:

wget 'http://localhost/my/script/file.php?id=19&myvar=xyz'

OR:

或者:

wget -q -O - "http://localhost/my/script/file.php?id=19&myvar=xyz"

-- Accessing the variables in php --

-- 在 php 中访问变量 --

In both option 1 & 2 you access these parameters like this:

在选项 1 和 2 中,您可以像这样访问这些参数:

$id = $_GET["id"];
$myvar = $_GET["myvar"];