thinkphp使用命令行运行某个方法

时间:2020-05-08 17:56:27   收藏:0   阅读:213

到项目目录然后运行指令

php public/index.php api/index/test

带参数运行

php public/index.php api/index/test --test1=1 --test2=2

 

解析参数

    public function test()
    {
        $argv = $_SERVER["argv"];
        $argc = $this->parseArgs($argv);
        var_dump($argc);exit;
    }

    /*
     * 参数提取
     * */
    public function parseArgs($argv)
    {
        array_shift($argv);
        $out = array();
        foreach ($argv as $arg) {
            if (substr($arg, 0, 2) == ‘--‘) {
                $eqPos = strpos($arg, ‘=‘);
                if ($eqPos === false) {
                    $key = substr($arg, 2);
                    $out[$key] = isset($out[$key]) ? $out[$key] : true;
                } else {
                    $key = substr($arg, 2, $eqPos - 2);
                    $out[$key] = substr($arg, $eqPos + 1);
                }
            } else if (substr($arg, 0, 1) == ‘-‘) {
                if (substr($arg, 2, 1) == ‘=‘) {
                    $key = substr($arg, 1, 1);
                    $out[$key] = substr($arg, 3);
                } else {
                    $chars = str_split(substr($arg, 1));
                    foreach ($chars as $char) {
                        $key = $char;
                        $out[$key] = isset($out[$key]) ? $out[$key] : true;
                    }
                }
            } else {
                $out[] = $arg;
            }
        }
        return $out;
    }
    

命令行里无法使用session和cookie

 

原文:https://www.cnblogs.com/codeninja/p/12851447.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!