融合三方

2018-05-31 17:19 更新

ApiAdmin允许你便捷的接入第三方API,这一章将以ApiAdmin接入另一个ApiAdmin为例来阐述融合第三方。

声明

  • 融合API需要具备编程基础,需要自己写SDK,或者修改SDK
  • 接入三方API需要遵循ApiAdmin的部分规则,但是可以很大程度的自定义

运行流程

目录结构

目录结构只是建议的目录安排,如果你觉得不方便,完全可以自定义,包括文件命名和文件的个数,都可以自定义,只要成功接入三方SDK就可以(很多时候可以直接使用第三方提供的SDK稍作改动即可)!

Home
├─ApiStore                    接口仓库
│ ├─ApiSDK                    接口SDK
│ │ ├─ApiAdmin
│ │ │ ├─AuthSign.class.php    ApiAdmin秘钥计算处理
│ │ │ └─Http.class.php        ApiAdmin的HTTP请求处理
│ │ └─ApiAdminSDK.class.php   调用ApiAdmin的函数库
│ └─ApiAdmin.class.php        将ApiAdmin的接口映射成框架内部函数

开发编码

下面将给出各个文件的代码示例。

<?php
namespace Home\ApiStore\ApiSDK\ApiAdmin;
/**
 * ApiAdmin秘钥计算
 * @since   2017/04/20 创建
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */
class AuthSign {
    private $version;
    private $appInfo;


    public function __construct($version, $appInfo) {
        $this->version = $version;
        $this->appInfo = $appInfo;
    }


    public function getHeader($accessToken = '', $userToken = false) {
        $header['version'] = $this->version;
        if ($accessToken) {
            $header['access-token'] = $accessToken;
        }
        if ($userToken) {
            $header['user-token'] = $userToken;
        }
        return $header;
    }


    public function getAccessTokenData() {
        $data['app_id'] = $this->appInfo['appId'];
        $data['app_secret'] = $this->appInfo['appSecret'];
        $data['device_id'] = 'zuAdmin';
        $data['rand_str'] = md5(rand(1, 10000) . microtime());
        $data['timestamp'] = time();
        $sign = $this->getSignature($data);
        $data['signature'] = $sign;
        return $data;
    }


    /**
     * 获取身份秘钥
     * @param array $data
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     * @return string
     */
    private function getSignature($data) {
        ksort($data);
        $preStr = http_build_query($data);


        return md5($preStr);
    }
}

<?php
/**
 * ApiAdmin通讯类
 * @since   2017/04/20 创建
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */
namespace Home\ApiStore\ApiSDK\ApiAdmin;
use Home\ORG\Response;
use Home\ORG\ReturnCode;
class Http {


    public static function get($url, $header = array()) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


        if($header){
            $newHeader = array();
            foreach ($header as $key => $item) {
                $newHeader[] = $key.':'.$item;
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, $newHeader);
        }


        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $response = curl_exec($ch);


        if (curl_errno($ch)) {
            Response::error(ReturnCode::CURL_ERROR, curl_error($ch));
        }
        curl_close($ch);
        $resArr = json_decode($response, true);


        return $resArr;
    }


    public static function post($url, $header = array(), $body = array()) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


        if($header){
            $newHeader = array();
            foreach ($header as $key => $item) {
                $newHeader[] = $key.':'.$item;
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, $newHeader);
        }


        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }


        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));


        $response = curl_exec($ch);


        if (curl_errno($ch)) {
            Response::error(ReturnCode::CURL_ERROR, curl_error($ch));
        }
        curl_close($ch);
        $resArr = json_decode($response, true);


        return $resArr;
    }


}

<?php
/**
 * 接口函数库
 * @since   2017/05/10 创建
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */
namespace Home\ApiStore\ApiSDK;
use Home\ApiStore\ApiSDK\ApiAdmin\AuthSign;
use Home\ApiStore\ApiSDK\ApiAdmin\Http;
class ApiAdminSDK {
    private $method;  //接口名称
    private $baseUrl = '';
    private $accessTokenHash = '';
    private $version = 'v1.0';
    private $appInfo = array();


    /**
     * ApiAdminSDK constructor.
     * @param string $method  接口名称
     * @param string $appInfo 应用信息
     */
    public function __construct($method, $appInfo) {
        $this->method = $method;
        $this->appInfo = $appInfo;
    }


    public function updateAccessToken() {
        $cacheKey = $this->appInfo['appId'] . '_access_token';
        S($cacheKey, null);
    }


    public function getHeader($userToken = '') {
        $signObj = new AuthSign($this->version, $this->appInfo);
        $accessToken = $this->getAccessToken();


        return $signObj->getHeader($accessToken, $userToken);
    }


    public function getAccessToken() {
        $cacheKey = $this->appInfo['appId'] . '_access_token';
        $accessToken = S($cacheKey);
        if (!$accessToken) {
            $signObj = new AuthSign($this->version, $this->appInfo);
            $data = $signObj->getAccessTokenData();
            $queryStr = http_build_query($data);
            $url = $this->baseUrl . $this->accessTokenHash . '?' . $queryStr;
            $header = $signObj->getHeader();
            $returnArr = Http::get($url, $header);
            if($returnArr['code'] == 1){
                $accessToken = $returnArr['data']['access_token'];
            }
        }


        return $accessToken;
    }


    /**
     * 处理URL,当需要GET请求,请传入GET参数数组
     * @param $data
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     * @return string
     */
    public function buildUrl($data = array()) {
        if ($data) {
            $queryStr = '?';
            $queryStr .= http_build_query($data);
        } else {
            $queryStr = '';
        }


        return $this->baseUrl . $this->method . $queryStr;
    }
}

ApiAdmin.class.php是系统自动识别的,所以有部分配置是必须严格配置的,这也是唯一一个ApiAdmin要求注意的地方

<?php
/**
 * ApiAdmin接口
 * @since   2017-04-27
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */
namespace Home\ApiStore;
use Home\ApiStore\ApiSDK\ApiAdmin\Http;
use Home\ApiStore\ApiSDK\ApiAdminSDK;
use Home\ORG\Response;
use Home\ORG\ReturnCode;
class ApiAdmin {
    public static $apiName = 'ApiAdmin接口'; //这个是必须配置正确的,系统扫描文件的时候需要读取这个配置


    private $authInfo = null;


    public function charge($param) {
        if (is_null($this->authInfo)) {
            $this->authInfo = M('ApiStoreAuth')->where(array('id' => $param['API_ADMIN_API_STORE_AUTH_ID'], 'status' => 1))->find();
            if (!$this->authInfo) {
                Response::error(ReturnCode::NOT_EXISTS, '没有可用的Key');
            }
        }


        $obj = new ApiAdminSDK('58fb7f6e9e07c', $this->authInfo);
        $url = $obj->buildUrl(array());


        RE:
        $header = $obj->getHeader();
        $res = Http::get($url, $header);
        if ($res['code'] == ReturnCode::ACCESS_TOKEN_TIMEOUT) {
            $obj->updateAccessToken();
            goto RE;
        }
        if ($res['code'] == ReturnCode::SUCCESS) {
            return array(
                'code' => $res['code'],
                'data' => array()
            );
        } else {
            Response::debug($res);
        }
    }
}

后台配置

进入三方接口,刷新接口列表,系统会自动获取你刚刚开发的三方SDK。

如果有AppID和AppSecret,那么你就需要在秘钥配置上添加,同时在接口上适配上相应的秘钥对

最后声明

系统会自动获取你配置的秘钥对,调用相对应SDK完成接口接入,以及接口请求。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号