php $_SERVER['argv'] 带有 HTTP GET 和 CLI 问题

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

$_SERVER['argv'] with HTTP GET and CLI issue

phpgetquery-stringcommand-line-interface

提问by gremo

I'm trying to write down a script to fetch some online data; script should be invoked either by a cron job or php cliand with standard GET HTTP request. As stated on PHP website $_SERVER['argv']should fit my needs:

我正在尝试写一个脚本来获取一些在线数据;脚本应由cron 作业或 php cli以及标准的GET HTTP 请求调用。正如 PHP 网站上所述,$_SERVER['argv']应该符合我的需求:

Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string.

传递给脚本的参数数组。当脚本在命令行上运行时,这将提供对命令行参数的 C 样式访问。当通过 GET 方法调用时,这将包含查询字符串。

However i can't get it to work with standard HTTP GET request. $_SERVER['argv']is not setted. What i'm missing?

但是我不能让它与标准的 HTTP GET 请求一起工作。$_SERVER['argv']未设置。我缺少什么?

<?php
    // jobs/fetch.php
    var_dump($_SERVER['argv']);
?>

CLI outputphp jobs/fetch.php -a -bhello:

命令行输出php jobs/fetch.php -a -bhello

array(3) {
  [0]=>
  string(14) "jobs/fetch.php"
  [1]=>
  string(2) "-a"
  [2]=>
  string(7) "-bhello"
}

GET outputjobs/fetch.php?a=&b=hello:

获取输出jobs/fetch.php?a=&b=hello

Notice: Undefined index: argv in jobs/fetch.php.

注意:未定义索引:jobs/fetch.php 中的 argv。

回答by drew010

The manual didn't state this very well, but, if you want $_SERVER['argc'], $_SERVER['argv'], $argc, $argvto be registered when you are not running in CLImode, then the php.inivalue register_argc_argvneeds to be enabled in php.ini (off by default [for performance reasons]).

该手册并没有说明这一点非常好,但是,如果你想$_SERVER['argc']$_SERVER['argv']$argc$argv要当你不是在运行注册CLI模式,则php.iniregister_argc_argv需要默认情况下,在php.ini(关启用[出于性能的考虑] )。

You could do the following to get argv, or query string args depending on how the script is running:

argv根据脚本的运行方式,您可以执行以下操作来获取或查询字符串参数:

if (php_sapi_name() == 'cli') {
    $args = $_SERVER['argv'];
} else {
    parse_str($_SERVER['QUERY_STRING'], $args);
}

Here are some details from php.ini:

以下是来自php.ini

; This directive determines whether PHP registers $argv & $argc each time it
; runs. $argv contains an array of all the arguments passed to PHP when a script
; is invoked. $argc contains an integer representing the number of arguments
; that were passed when the script was invoked. These arrays are extremely
; useful when running scripts from the command line. When this directive is
; enabled, registering these variables consumes CPU cycles and memory each time
; a script is executed. For performance reasons, this feature should be disabled
; on production servers.
; Note: This directive is hardcoded to On for the CLI SAPI
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/register-argc-argv

See also http://www.php.net/manual/en/reserved.variables.argv.phpand parse_str().

另请参阅http://www.php.net/manual/en/reserved.variables.argv.phpparse_str()

回答by gremo

You will have to use $_GETor$_SERVER['argv']depending on how your script is called. Neither one is used for both.

您将不得不使用$_GET$_SERVER['argv']取决于您的脚本的调用方式。两者都不使用任何一种。

For example:

例如:

if(!empty($_SERVER['argv'][0]) {
  $a = $_SERVER['argv'][1];
  $b = $_SERVER['argv'][2];
} else {
  $a = $_GET['a'];
  $b = $_GET['b'];
}