码迷,mamicode.com
首页 > Windows程序 > 详细

api 测试auth问题的php脚本

时间:2019-10-21 11:35:14      阅读:122      评论:0      收藏:0      [点我收藏+]

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

<?php

/**
host
dev http://192.168.163.132|web.hudong.cn
master http://hudong.qianfanyun.com

auth
url /session/create
password  xxxx
username xxxxx
return /\S+/
http
get /test

 */
class Gapi
{

    public $host = '';

    public $url = '';

    public $type = '';

    public $header = [];

    public $params = [];

    public $data = [];

    public function __construct()
    {
        if (isset($_GET['c'])) {
            $config = file_get_contents("./{$_GET['c']}");
        } else {
            $config = file_get_contents("./config");
        }
        preg_match_all('/\S+(\r|\n[\S|\t]+.*)+/', $config, $contentOneArray, PREG_PATTERN_ORDER);
        foreach ($contentOneArray[0] as $key => $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 < count($res[0]); $i++) {
            if ($i % 2 == 1) {
                $arr[$key][$res[0][$i]] = "";
            } else {
                $arr[$key][$res[0][$i - 1]] = $res[0][$i];
            }
        }
        return $arr;
    }

    public function run()
    {
        $this->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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!