$ _SERVER ['argv']带有HTTP GET和CLI问题 [英] $_SERVER['argv'] with HTTP GET and CLI issue
问题描述
我试图写下一个脚本来获取一些在线数据;脚本应该由 cron作业或php cli 和标准的 GET HTTP请求调用。正如PHP网站所述 $ _ SERVER ['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:
传递给脚本的参数数组。当脚本在
命令行上运行时,可以通过C风格访问命令行
参数。当通过GET方法调用时,它将包含
查询字符串。
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']
未设置。
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输出 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 = hello
:
GET output jobs/fetch.php?a=&b=hello
:
$ b $
推荐答案
注意:未定义的索引:argv在jobs / fetch.php中。手册没有说明这一点,但是,如果你想 $ _ SERVER ['argc']
, $ _ SERVER ['argv']
, $ argc
, $ argv
当您没有在 CLI
模式,然后 php.ini
值 register_argc_argv (由于性能原因,默认关闭)。
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]).
您可以根据脚本的运行方式执行以下操作来获取 argv
或查询字符串参数:
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);
}
以下是 php.ini
:
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
另见 http://www.php.net/manual/en/reserved.variables.argv.php 和 parse_str()。
这篇关于$ _SERVER ['argv']带有HTTP GET和CLI问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!