簡體   English   中英

如何將參數從 bash 傳遞到 php 腳本?

[英]How to pass parameters from bash to php script?

我已經完成了運行 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

我從 bash 運行 php 腳本,如下所示:

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

稱它為:

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

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

parse_str($argv[1]);

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


更高級的處理可能需要getopt() ,但parse_str()是讓它工作的快速'n'dirty 方式。

您不能將 GET 查詢參數傳遞給 PHP 命令行界面。 將 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):

php myfile.php -- argument1 argument2

-- 選項一:php-cgi --

使用 'php-cgi' 代替 'php' 來運行你的腳本。 這是最簡單的方法,因為您無需專門修改 php 代碼即可使用它:

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

-- 選項 2:如果您有 web 服務器 --

如果 php 文件位於 web 服務器上,則可以在命令行上使用“wget”:

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

或者:

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

-- 訪問 php 中的變量 --

在選項 1 和 2 中,您可以像這樣訪問這些參數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM