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

发布于 2025-01-05 01:21:49 字数 803 浏览 1 评论 0 原文

我正在尝试编写一个脚本来获取一些在线数据;脚本应该通过 cron 作业或 php cli 并使用标准 GET HTTP 请求来调用。正如 PHP 网站 $_SERVER['argv'] 上所述应该满足我的需求:

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

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

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

CLI 输出 php jobs/fetch.php -a -bhello:

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

GET 输出 jobs/fetch.php?a=&b =你好

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

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

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.

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日暮斜阳 2025-01-12 01:21:49

手册没有很好地说明这一点,但是,如果你想要 $_SERVER['argc'], $_SERVER['argv'], $argc , $argv 当你不是在CLI模式下运行时被注册,那么php.iniregister_argc_argv 需要在 php.ini 中启用(默认情况下关闭[出于性能原因])。

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

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

以下是 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

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

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

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

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

Here are some details from 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.php and parse_str().

一指流沙 2025-01-12 01:21:49

您必须使用 $_GET $_SERVER['argv'],具体取决于脚本的调用方式。两者均不使用任何一种。

例如:

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

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

For example:

if(!empty($_SERVER['argv'][0]) {
  $a = $_SERVER['argv'][1];
  $b = $_SERVER['argv'][2];
} else {
  $a = $_GET['a'];
  $b = $_GET['b'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文