gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:29 24 4
gpt4 key购买 nike

我正在尝试写一个脚本来获取一些在线数据;脚本应由 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.

但是我无法让它与标准的 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"
}

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

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

最佳答案

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

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

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.phpparse_str()

关于php - $_SERVER ['argv' ] HTTP GET 和 CLI 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9270030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com