api 测试auth问题的php脚本

2021-05-19 05:30

阅读:419

标签:echo   post   word   nfa   脚本   this   tran   header   name   

 $value) {
            $this->data = array_merge($this->data, $this->buildOne($value));
        }
    }

    public function buildOne($content)
    {
        $arr = [];
        preg_match_all('/\S+/', $content, $res, PREG_PATTERN_ORDER);
        $key       = $res[0][0];
        $arr[$key] = [];
        for ($i = 1; $i buildHost();
        $this->buildAuth();
        $this->buildUrl();
        $res = $this->curl($this->type);
        if (preg_match('/^{(\w|\W)+}$/', $res)) {
            echo $this->decodeUnicode(json_encode(json_decode($res), JSON_PRETTY_PRINT));
        } else {
            echo $res;
        }
    }

    public function decodeUnicode($str)
    {
        return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return iconv("UCS-2BE","UTF-8",pack("H*", $matches[1]));'), $str);
    }

    public function buildAuth()
    {
        $auth      = $this->data['auth'];
        $this->url = $this->host . $auth['url'];
        $start     = isset($auth['start']) ? $auth['start'] : '';
        $return    = $auth['return'];
        unset($auth['url']);
        unset($auth['return']);
        $this->params = $auth;
        $postContent  = $this->curl('post');
        preg_match($return, $postContent, $res);
        if (!isset($res[0])) {
            throw new \Exception("auth error:\n" . $postContent . "\n" . json_encode($this), 4324);
        }
        if ($start == "Bearer") {
            $start .= ' ';
        }
        array_push($this->header, "Authorization: " . $start . $res[0]);
    }

    public function buildHost()
    {
        $key = "dev";
        if (
            isset($_GET["t"])
            &&
            $_GET["t"] == 'm'
        ) {
            $key = 'master';
        }
        $dev        = explode('|', $this->data['host'][$key]);
        $this->host = $dev[0];
        if (isset($dev[1])) {
            array_push($this->header, "host:{$dev[1]}");
        }
    }

    public function buildUrl()
    {
        $http = $this->data['http'];
        foreach ($http as $key => $value) {
            $this->type = $key;
            break;
        }
        $this->url = $this->host . $http[$this->type];
        if ($this->type != "get") {
            $this->buildParams();
        }
    }

    public function buildParams()
    {
        $http = $this->data['http'];
        unset($http[$this->type]);
        $this->params = $http;
    }

    public function curl($type)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
        switch ($type) {
            case 'post':
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
                break;
            case 'put':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
                break;
            default:
                break;
        }
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return $file_contents;
    }

}

$gapi = new Gapi();
$gapi->run();

api 测试auth问题的php脚本

标签:echo   post   word   nfa   脚本   this   tran   header   name   

原文地址:https://www.cnblogs.com/zhsngq/p/11712213.html


评论


亲,登录后才可以留言!