将变量传递给从命令行运行的 php 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6826718/
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
Pass variable to php script running from command line
提问by hd.
I have a PHP file that is needed to be run from command line (via crontab). I need to pass type=daily
to the file but I don't know how. I tried:
我有一个需要从命令行(通过 crontab)运行的 PHP 文件。我需要传递type=daily
给文件,但我不知道如何传递。我试过:
php myfile.php?type=daily
but this error was returned:
但是返回了这个错误:
Could not open input file: myfile.php?type=daily
无法打开输入文件:myfile.php?type=daily
What can I do?
我能做什么?
回答by PtPazuzu
The ?type=daily
argument (ending up in the $_GET
array) is only valid for web-accessed pages.
该?type=daily
参数(在结束了$_GET
阵列)仅适用于网络访问的页面。
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
).
您需要像这样调用它php myfile.php daily
并从$argv
数组中检索该参数(这将是$argv[1]
,因为$argv[0]
将是myfile.php
)。
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:
如果页面也用作网页,您可以考虑两种选择。使用 shell 脚本和 wget 访问它并从 cron 调用它:
#!/bin/sh
wget http://location.to/myfile.php?type=daily
Or check in the php file whether it's called from the commandline or not:
或者检查 php 文件是否从命令行调用它:
if (defined('STDIN')) {
$type = $argv[1];
} else {
$type = $_GET['type'];
}
(Note: You'll probably need/want to check if $argv
actually contains enough variables and such)
(注意:您可能需要/想要检查是否$argv
实际包含足够的变量等)
回答by Christoph Fink
Just pass it as normal parameters and access it in PHP using the $argv
array.
只需将它作为普通参数传递并使用$argv
数组在 PHP 中访问它。
php myfile.php daily
and in myfile.php
并在 myfile.php 中
$type = $argv[1];
回答by fboes
These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar"
into the well known $_GET
-array:
这些行会将 CLI 调用的参数转换为php myfile.php "type=daily&foo=bar"
众所周知的$_GET
-array:
if (!empty($argv[1])) {
parse_str($argv[1], $_GET);
}
Though it is rather messy to overwrite the global $_GET
-array, it converts all your scripts quickly to accept CLI arguments.
尽管覆盖全局$_GET
数组相当麻烦,但它可以快速转换所有脚本以接受 CLI 参数。
See http://php.net/manual/en/function.parse-str.phpfor details.
回答by Rupesh Wankhede
Using getopt() function we can also read parameter from command line just pass value with php running command
使用 getopt() 函数,我们还可以从命令行读取参数,只需使用 php 运行命令传递值
php abc.php --name=xyz
php abc.php --name=xyz
abc.php
abc.php
$val = getopt(null, ["name:"]);
print_r($val); // output: ['name' => 'xyz'];
回答by Subdigger
parameters send by index like other application
像其他应用程序一样通过索引发送参数
php myfile.php type=daily
and then you can gat them like this
然后你可以像这样控制它们
<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>
回答by easyaspi
Save this code in file myfile.php
and run as php myfile.php type=daily
将此代码保存在文件中myfile.php
并作为php myfile.php type=daily
<?php
$a = $argv;
$b = array();
if (count($a) === 1) exit;
foreach ($a as $key => $arg) {
if ($key > 0) {
list($x,$y) = explode('=', $arg);
$b["$x"] = $y;
}
}
?>
If you add var_dump($b);
before the ?>
tag, you will see that the array $b
contains type => daily
.
如果var_dump($b);
在?>
标记之前添加,您将看到数组$b
包含type => daily
.
回答by Francisco Luz
I strongly recommend the use of getopt.
我强烈推荐使用 getopt。
Documentation at http://php.net/manual/en/function.getopt.php
http://php.net/manual/en/function.getopt.php 上的文档
If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface
如果您想打印出您的选项的帮助,请查看https://github.com/c9s/GetOptionKit#general-command-interface
回答by K3---rnc
You could use what sep16 on php.net recommends:
<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);
?>
It behaves exactly like you'd expect with cgi-php.
它的行为与您对 cgi-php 的期望完全一样。
$ php -f myfile.php type=daily a=1 b[]=2 b[]=3
will set $_GET['type']
to 'daily'
, $_GET['a']
to '1'
and $_GET['b']
to array('2', '3')
.
将设置$_GET['type']
到'daily'
,$_GET['a']
到'1'
和$_GET['b']
到array('2', '3')
。
回答by hamboy75
You can use the next code to work with command line and webbrowser. Put this code above of your php code. It creates a $_GET variable for each command line parameter.
您可以使用下一个代码来处理命令行和网络浏览器。将此代码放在您的 php 代码之上。它为每个命令行参数创建一个 $_GET 变量。
In your code you only need to check for $_GET variables then, not worrying about if script is called from webbrowser or command line.
在您的代码中,您只需要检查 $_GET 变量,而不必担心是否从 webbrowser 或命令行调用脚本。
if(isset($argv))
foreach ($argv as $arg) {
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
}
回答by Sam Prasad
Just pass it as parameters as follows:
只需将其作为参数传递如下:
php test.php one two three
and inside test.php:
在 test.php 中:
<?php
if(isset($argv))
{
foreach ($argv as $arg)
{
echo $arg;
echo "\r\n";
}
}
?>