問題描述
我有一個需要從命令行運行的 PHP 文件(通過 crontab一>).我需要將 type=daily
傳遞給文件,但我不知道如何.我試過了:
I have a PHP file that is needed to be run from the command line (via crontab). I need to pass type=daily
to the file, but I don't know how. I tried:
php myfile.php?type=daily
但是返回了這個錯誤:
無法打開輸入文件:myfile.php?type=daily
Could not open input file: myfile.php?type=daily
我能做什么?
推薦答案
?type=daily
參數(以 $_GET
數組結尾)僅對網頁訪問頁面.
The ?type=daily
argument (ending up in the $_GET
array) is only valid for web-accessed pages.
您需要像 php myfile.php daily
一樣調用它,并從 $argv
數組(這將是 $argv[1]
,因為 $argv[0]
將是 myfile.php
).
You'll need to call it like php myfile.php daily
and retrieve that argument from the $argv
array (which would be $argv[1]
, since $argv[0]
would be myfile.php
).
如果頁面也用作網頁,您可以考慮兩種選擇.使用 shell 腳本和 Wget 訪問它,然后從 cron:
If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and Wget, and call that from cron:
#!/bin/sh
wget http://location.to/myfile.php?type=daily
或者檢查PHP文件是否從命令行調用:
Or check in the PHP file whether it's called from the command line or not:
if (defined('STDIN')) {
$type = $argv[1];
} else {
$type = $_GET['type'];
}
(注意:您可能需要/想要檢查 $argv
是否確實包含足夠的變量等)
(Note: You'll probably need/want to check if $argv
actually contains enough variables and such)
這篇關于將變量傳遞給從命令行運行的 PHP 腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!