Php $服务器[';argv';]存在HTTP GET和CLI问题

Php $服务器[';argv';]存在HTTP GET和CLI问题,php,get,query-string,command-line-interface,Php,Get,Query String,Command Line Interface,我试图写一个脚本来获取一些在线数据;脚本应通过cron作业或php cli调用,并使用标准的GET HTTP请求调用。如PHP网站$\u服务器['argv']所述,应满足我的需要: 传递给脚本的参数数组。当脚本在服务器上运行时 命令行,这将提供对命令行的C样式访问 参数。当通过GET方法调用时,它将包含 查询字符串 但是,我无法让它与标准的HTTP get请求一起工作$\未设置服务器['argv']。我错过了什么 <?php // jobs/fetch.php var_du

我试图写一个脚本来获取一些在线数据;脚本应通过cron作业或php cli调用,并使用标准的GET HTTP请求调用。如PHP网站
$\u服务器['argv']
所述,应满足我的需要:

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

但是,我无法让它与标准的HTTP get请求一起工作<代码>$\未设置服务器['argv']。我错过了什么

<?php
    // jobs/fetch.php
    var_dump($_SERVER['argv']);
?>
获取输出
jobs/fetch.php?a=&b=hello

array(3) {
  [0]=>
  string(14) "jobs/fetch.php"
  [1]=>
  string(2) "-a"
  [2]=>
  string(7) "-bhello"
}
注意:未定义的索引:jobs/fetch.php中的argv


根据脚本的调用方式,您必须使用
$\u GET
$\u SERVER['argv']
。两者都不适用

例如:

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

该手册没有很好地说明这一点,但是,如果您希望
$\u服务器['argc']
$\u服务器['argv']
$argc
$argv
在未在
CLI
模式下运行时注册,则需要在php.ini中启用
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
另见和