[英]How to pass parameters from bash to php script?
I have done aa bash script which run php script.我已经完成了运行 php 脚本的 bash 脚本。 It works fine without parameters but when I add parameters (id and url), there are some errors:没有参数它可以正常工作,但是当我添加参数(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
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()是让它工作的快速'n'dirty 方式。
You can't pass GET query parameters to the PHP command line interface.您不能将 GET 查询参数传递给 PHP 命令行界面。 Either pass the arguments as standard command line arguments and use the $argc
and $argv
globals to 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.将 arguments 作为标准命令行 arguments 传递并使用$argc
和$argv
全局变量来读取它们,或者(如果必须使用 GET/POST 参数)通过 curl/wget 调用脚本并以这种方式传递参数——假设你有该脚本可通过本地 web 服务器访问。
This is how you can pass arguments to be read by $argc
and $argv
(the -- indicates that all subsequent arguments should go to the script and not to the PHP interpreter binary): This is how you can pass arguments to be read by $argc
and $argv
(the -- indicates that all subsequent arguments should go to the script and not to the PHP interpreter binary):
php myfile.php -- argument1 argument2
-- Option 1: php-cgi -- -- 选项一:php-cgi --
Use 'php-cgi' in place of 'php' to run your script.使用 'php-cgi' 代替 'php' 来运行你的脚本。 This is the simplest way as you won't need to specially modify your php code to work with it:这是最简单的方法,因为您无需专门修改 php 代码即可使用它:
php-cgi -f /my/script/file.php id=19 myvar=xyz
-- Option 2: if you have a web server -- -- 选项 2:如果您有 web 服务器 --
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"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.