Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Command line 通过命令行调用PHP脚本时发送请求参数_Command Line_Php - Fatal编程技术网

Command line 通过命令行调用PHP脚本时发送请求参数

Command line 通过命令行调用PHP脚本时发送请求参数,command-line,php,Command Line,Php,当您通过浏览器运行PHP脚本时,它看起来像 http://somewebsite.com/yourscript?param1=val1&param2=val2. 我试图通过命令行实现同样的功能,而不必重写脚本来接受argv,而不是$\u请求。有没有办法做到这一点: php yourscript.php?param1=val1&param2=val2 $ php test.php --param1=val1 --param2=val2 param1 = val1 param2

当您通过浏览器运行PHP脚本时,它看起来像

http://somewebsite.com/yourscript?param1=val1&param2=val2.
我试图通过命令行实现同样的功能,而不必重写脚本来接受
argv
,而不是
$\u请求。有没有办法做到这一点:

php yourscript.php?param1=val1&param2=val2 
$ php test.php --param1=val1 --param2=val2
param1 = val1
param2 = val2

这样您发送的参数就会显示在
$\u请求变量中?

不,没有简单的方法可以实现这一点。web服务器将拆分请求字符串并将其传递给PHP解释器,然后PHP解释器将其存储在
$\u request
数组中

如果从命令行运行,并且希望接受类似的参数,则必须自己解析它们。命令行传递参数的语法与HTTP完全不同。你可能想调查一下

对于不考虑用户错误的暴力方法,您可以尝试以下代码段:

<?php
foreach( $argv as $argument ) {
        if( $argument == $argv[ 0 ] ) continue;

        $pair = explode( "=", $argument );
        $variableName = substr( $pair[ 0 ], 2 );
        $variableValue = $pair[ 1 ];
        echo $variableName . " = " . $variableValue . "\n";
        // Optionally store the variable in $_REQUEST
        $_REQUEST[ $variableName ] = $variableValue;
}

如果不想修改正在运行的脚本,可以使用In-B参数指定参数,以指定要在输入文件之前运行的代码。但在这种情况下,还必须添加-F标记以指定输入文件:

php -B "\$_REQUEST = array('param1' => 'val1', 'param2' => 'val2');" -F yourscript.php

我编写了一个短函数来处理这种情况——如果存在命令行参数,并且$\u请求数组为空(即,当您从命令行而不是通过web界面运行脚本时),它会在key=value对中查找命令行参数

Argv2Request($argv);

print_r($_REQUEST);

function Argv2Request($argv) {
    /*
      When $_REQUEST is empty and $argv is defined,
      interpret $argv[1]...$argv[n] as key => value pairs
      and load them into the $_REQUEST array

      This allows the php command line to subsitute for GET/POST values, e.g.
      php script.php animal=fish color=red number=1 has_car=true has_star=false
     */


    if ($argv !== NULL && sizeof($_REQUEST) == 0) {
        $argv0 = array_shift($argv); // first arg is different and is not needed

        foreach ($argv as $pair) {
            list ($k, $v) = split("=", $pair);
            $_REQUEST[$k] = $v;
        }
    }
}
函数注释中建议的示例输入为:

php script.php animal=fish color=red number=1 has_car=true has_star=false
这将产生输出:

Array
(
    [animal] => fish
    [color] => red
    [number] => 1
    [has_car] => true
    [has_star] => false
)

我不能对此负责,但我在我的引导文件中采用了这一点:

// Concatenate and parse string into $_REQUEST
if (php_sapi_name() === 'cli') {
    parse_str(implode('&', array_slice($argv, 1)), $_REQUEST);
}
从命令行执行PHP文件时:

php yourscript.php param1=val1 param2=val2

以上内容将把键和值插入$\u请求,以便以后检索。

谢谢,我最后做了类似的事情,效果很好。$需要转义,否则您的shell将用$\u请求替换空字符串,并给出错误:PHP Parse error:syntax error,如果不转义$,第1行的命令行开始代码中意外出现的“=”将无法工作。这是:
php-B“\$\u请求=数组('param1'=>'val1','param2'=>'val2');”-F yourscript.php