首页 > 编程语言 >PHP对接快手平台

PHP对接快手平台

时间:2023-03-08 18:55:58浏览次数:50  
标签:code 快手 res 对接 param access token curl PHP

学习记录 留作参考
祝君好运

实际业务中需要使用PHP对接快手平台,而快手官方只给出了一个JAVA的SDK。结合网络上找到的一些代码,自己又整理了一份。

参考内容:
快手开放平台新手指南
CSDN相关使用PHP对接快手平台的博客

<?php

/**
 * 业务方法
 * 
 * 需要自行配置快手账号信息,来获得授权。
 * 本文使用文件记录访问快手接口的token信息,需要自行配置保存目录,默认为当前目录。
 */
class index
{
    public $ksToolObj = null;

    public function __construct()
    {
        // 从配置文件中读取快手平台的账号信息
        $appKey = config::get('appKey');
        $appSecret = config::get('appSecret');
        $signSecret = config::get('signSecret');
        $secretKey = config::get('secretKey');

        $this->ksToolObj = new ksTool($appKey, $appSecret, $signSecret, $secretKey);

        // 配置一个保存日志缓存文件的目录。
        if (!defined("STORAGE_DIR")) {
            define('STORAGE_DIR', './');
        }
    }

    /**
     * code换取token的方法,所有的请求都需要access_token
     * @param string $code 用户的授权code
     */
    public function code($code)
    {
        try {
            $this->ksToolObj->code2Token($code);
        } catch (\Exception $e) {
            print_r($e->getMessage());
        }
    }

    /**
     * 示例方法,实际业务
     */
    public function someMethod()
    {
        try {
            $api = 'open.funds.financial.bill.batch.detail';
            $param = array();
            $param['cursor'] = '';
            $param['settlementStartTime'] = strtotime('2023-1-1') * 1000;
            $param['settlementEndTime'] = strtotime('2023-1-4') * 1000;

            $res = $this->ksToolObj->ksData($api, $param, 'get');
            $this->__saveFileStorage($res); // 为了方便查看效果而记录文件日志
            print_r($res);
        } catch (\Exception $e) {
            print_r($e->getMessage());
        }
    }

    // 记录文件日志
    private function __saveFileStorage($res)
    {
        $logStr = date('Y-m-d H:i:s') . "\n" . var_export($res, true) . "\n\n";
        file_put_contents(STORAGE_DIR . './ks.log', $logStr, FILE_APPEND);
    }
}


class ksTool
{
    public $appKey;
    public $appSecret;
    public $signSecret;
    public $secretKey;
    public $urlCommon = 'https://openapi.kwaixiaodian.com';

    // 通过接口获取的token
    public $access_token = '';

    public function __construct($appKey, $appSecret, $signSecret, $secretKey = '')
    {
        $this->appKey = $appKey;
        $this->appSecret = $appSecret;
        $this->signSecret = $signSecret;
        $this->secretKey = $secretKey;
    }

    /**
     * 通过code获取 refresh_token和access_token (调用次数比较少)
     * @param string $code 测试用户或者授权用户的授权code
     * @return array
     * @throws Exception
     */
    public function code2Token($code)
    {
        $param['app_id'] = $this->appKey;
        $param['app_secret'] = $this->appSecret;
        $param['grant_type'] = 'code';
        $param['code'] = $code;

        $url = $this->urlCommon . '/oauth2/access_token?' . http_build_query($param, '', '&');
        $res = $this->requestGet($url);

        if ($res && $res['result'] === 1) {
            $setRes = $this->__setStorageData($res);
            if ($setRes === false) {
                throw new Exception('缓存文件记录失败!');
            }
            return ['code' => 0, 'msg' => 'refresh token is success'];
        } else {
            $errStr = '';
            if ($res && is_array($res)) {
                foreach ($res as $key => $val) {
                    $errStr .= $key . ':' . $val . ' ';
                }
            }
            throw new Exception('通过code获取Token失败。响应:' . $errStr);
        }
    }

    /**
     * 通用的调用快手接口的方法
     * @param string $apiName 快手平台的接口
     * @param array $param 请求参数
     * @param string $method 请求方式
     * @return array
     * @throws Exception
     */
    public function ksData($apiName, $param = [], $method = 'get')
    {
        if (!$this->access_token) {
            $this->__getStorageToken();
        }

        $sendData['access_token'] = $this->access_token;
        $sendData['appkey'] = $this->appKey;
        $sendData['method'] = $apiName;
        $sendData['param'] = $param ? json_encode($param) : "{}";
        $sendData['signMethod'] = 'MD5';
        $sendData['timestamp'] = intval(microtime(true) * 1000);
        $sendData['version'] = 1;
        ksort($sendData);

        $paramStr = '';
        foreach ($sendData as $key => $val) {
            $paramStr .= $key . "=" . $val . "&";
        }
        $signStr = $paramStr . 'signSecret=' . $this->signSecret;

        $sendData['sign'] = md5($signStr);

        $url = $this->urlCommon . '/' . str_replace('.', '/', $sendData['method']);
        if (strtolower($method) === 'post') {
            $res = $this->requestPost($url, $sendData);
        } else {
            $url .= '?' . http_build_query($sendData, '', '&');
            $res = $this->requestGet($url);
        }
        return $res;
    }

    /**
     * 获取缓存到文件的token信息
     */
    private function __getStorageToken()
    {
        $fileStr = file_get_contents(STORAGE_DIR . '/kuaishou.conf.txt');
        if ($fileStr && $fileArr = json_decode($fileStr, true)) {
            if ($fileArr['access_expires'] > time() + 900) {
                $this->access_token = $fileArr['access_token'];
            } else if ($fileArr['refresh_expires'] > time()) {
                $this->__refreshAllToken($fileArr['refresh_token']);
            } else {
                throw new \Exception('refresh_token已经失效');
            }
        } else {
            throw new \Exception('未找到快手平台有效的token数据');
        }
    }

    /**
     * 保存快手接口返回的token信息
     * @param array $res 快手接口的响应信息
     * @return bool|int
     */
    private function __setStorageData($res)
    {
        $res['access_expires'] = time() + $res['expires_in']; // 计算access_token失效时间戳
        $res['refresh_expires'] = time() + $res['refresh_token_expires_in']; // 计算refresh_token失效时间戳
        $this->access_token = $res['access_token'];

        unset($res['expires_in'], $res['refresh_token_expires_in']);
        return file_put_contents(STORAGE_DIR . '/kuaishou.conf.txt', json_encode($res)); // 实际开发需要的变量
    }

    /**
     * 通过refresh_token来刷新access_token,同时refresh_token也会被刷新。
     * @param string $refreshToken
     * @return array
     * @throws Exception
     */
    private function __refreshAllToken($refreshToken)
    {
        $param['app_id'] = $this->appKey;
        $param['app_secret'] = $this->appSecret;
        $param['grant_type'] = 'refresh_token';
        $param['refresh_token'] = $refreshToken;

        $url = $this->urlCommon . '/oauth2/refresh_token?' . http_build_query($param, '', '&');
        $res = $this->requestGet($url);

        if ($res && $res['result'] === 1) {
            $setRes = $this->__setStorageData($res);
            if ($setRes === false) {
                throw new Exception('缓存文件记录失败!');
            }
            return ['code' => 0, 'msg' => 'refresh token is success'];
        } else {
            throw new Exception('刷新access_token失败。 error_msg:' . $res['error_msg']);
        }
    }

    /**
     * 发送GET请求
     */
    private function requestGet($url)
    {
        $headerArray = array("Content-type:application/x-www-form-urlencoded;", "Accept:application/json;charset=UTF-8");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true, 512, JSON_BIGINT_AS_STRING);
    }

    /**
     * 发送POST请求
     */
    private function requestPost($url, $data)
    {
        $data = json_encode($data);
        $headerArray = array("Content-type:application/x-www-form-urlencoded;", "Accept:application/json;charset=UTF-8");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true, 512, JSON_BIGINT_AS_STRING);
    }
}

标签:code,快手,res,对接,param,access,token,curl,PHP
From: https://www.cnblogs.com/liulog/p/17195731.html

相关文章

  • linux php安装gd扩展
    第一步:首先可以利用find/-namephp.ini查找一下,系统是否有php的配置文件,找到自己mysql的安装目录第二步:找到mysql的解压目录,cd apcache/php-5.6.33/ext/gd这是我的目......
  • JAVA的DigestUtils.md5Hex在php的写法
    php对接java接口时签名校验时,发现php的MD5加密跟java中的DigestUtils.md5Hex加密会不一样,需要统一输出;解决方法如下:/***16进制转string拼接*@paramarra......
  • PHP:json_encode序列化中文字符和json_decode反序列化object
    json_encode序列化<?php$name='你好';echojson_encode($name).PHP_EOL;//"\u4f60\u597d"echojson_encode($name,JSON_UNESCAPED_UNICODE).PHP_EOL;//"......
  • php快速排序和冒泡排序
    <?phpfunctionmaopao($arr){if(!is_array($arr)){return$arr;}$count=count($arr);if($count<=1){return$arr;}for......
  • PHP serialize & JSON 解析
    对于JSON(JavaScript​ ObjectNotation)大家应该不陌生,它是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLang......
  • php 将分钟前/小时前转为日期
    $startTime=date("Y-m-dH:i:s");if(strpos($date,'分钟前')!==false){$dtime=strstr($date,'分钟前',true);$mstr=$......
  • 织梦显示模板的PHP代码
    require_once("../include/common.inc.php");require(dirname(__FILE__)."/config.php");require_onceDEDEINC."/arc.partview.class.php";$pv=newPartView();......
  • 【黄啊码】浅谈PHP入门|如何学习PHP
    大家好,我是黄啊码,很多小白问我,怎么入门PHP,甚至连PHP能干嘛都不知道,今天啊码就具体来讲讲。什么是PHPPHP是一种开源的通用脚本语言,用于创建动态网页和应用程序。它可以运行在......
  • PHP过滤表单字段
    PHP过滤表单字段函数名  释义 介绍htmlspecialchars将与、单双引号、大于和小于号化成HTML格式&转成&amp; "转成"'转成'<转成<>转成>htmlentities()所有字符都转成......
  • php的pcre使用的NFA引擎可利用pcre.backtrack_limit(最大回溯次数)返回false绕过
    看P神的文章,学习web安全知识的前沿技术栈和各种tricks,这真是一个充满乐趣的过程。这是codebreaking上的第二题:pcrewaf首先先回顾一下php文件上传的相关代码:前端form表......