抖音小店获取accessToken-php实现

本文介绍了如何使用PHP来获取抖音小店的accessToken。首先,你需要按照入驻教程获取appkey, appSecret和shopid。接着,文章会分享用于获取accessToken的PHP工具方法源码。" 112333060,10534829,Anaconda安装与特性介绍,"['anaconda', 'python环境', '包管理', '环境配置']
部署运行你感兴趣的模型镜像

1.appkey,appSecret,shopid要根据入驻教程去获取设置

     //获取Access_Token 参考 https://op.jinritemai.com/docs/guide-docs/138/21
     private function getDYAccessToken($shopid){
        $res = json_decode(cache($shopid),true);//获取缓存
        if($res !== null && $res["access_token"] && $res["expires_in"]>time()){
            return $res['access_token'];
        }else {

            $method = 'token.create';
            $timestamp = time();
            $m = array(
                'code' => "",
                'grant_type' => 'authorization_self',
                'shop_id' => $shopid,
            );
            $accessToken = '';
            // 序列化参数
            $paramJson = $this->marshal($m);
            // 计算签名
            $signVal = $this->sign($this->appKey, $this->appSecret, $method, $timestamp, $paramJson);

            // 发起请求
            $res = $this->fetchdy($this->appKey, $this->host, $method, 
            $timestamp, $paramJson, $accessToken, $signVal);
            $arr = json_decode($res,true)['data'];
            if($arr !== null && isset($arr['access_token']) && $arr['access_token'] ){
                $arr['expires_in'] = $arr['expires_in'] +time();
                //将access_token 全局缓存 
                cache($shopid,json_encode($arr));
                return $arr['access_token'];
            }else {
                return self::showResCode('操作失败',["data"=>$arr,"flag"=>false]);;
            };
        }     
    }

2.工具方法源码

    // 调用Open Api,取回数据
    private function fetchdy(string $appKey, string $host, string $method,
     int $timestamp, string $paramJson, string $accessToken, string $sign):string 
    {
        $methodPath = str_replace('.', '/', $method);
        if($accessToken=="")
        {
            $url = $host . '/' . $methodPath .
                    '?method=' . urlencode($method) .
                    '&app_key=' . urlencode($appKey) .
                    '&timestamp=' . urlencode(strval($timestamp)) .
                    '&v=' . urlencode('2') .
                    '&sign_method=' . urlencode('hmac-sha256').
                    '&sign=' . urlencode($sign);
        }
        else
        {
            $url = $host . '/' . $methodPath .
                    '?method=' . urlencode($method) .
                    '&app_key=' . urlencode($appKey) .
                    '&access_token=' .urlencode($accessToken) .
                    '&timestamp=' . urlencode(strval($timestamp)) .
                    '&v=' . urlencode('2') .
                    '&sign_method=' . urlencode('hmac-sha256').
                    '&sign=' . urlencode($sign);
        }
        

        $opts = array('http' =>
            array(
                'method' => 'POST',
                'header' => "Accept: */*\r\n" .
                    "Content-type: application/json;charset=UTF-8\r\n",
                'content' => $paramJson
            )
        );
        $context = stream_context_create($opts);
        $result = file_get_contents($url, false, $context);
        return $result;
    }    

    // 序列化参数,入参必须为关联数组
    private function marshal(array $param): string {
        $this->rec_ksort($param); // 对关联数组中的kv,执行排序,需要递归
        $s = json_encode($param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); // 重新序列化,确保所有key按字典序排序
        // 加入flag,确保斜杠不被escape,汉字不被escape
        return $s;
    }

    // 关联数组排序,递归
    static function rec_ksort(array &$arr) {
        $kstring = true;
        foreach ($arr as $k => &$v) {
            if (!is_string($k)) {
                $kstring = false;
            }
            if (is_array($v)) {
                self::rec_ksort($v);
            }
        }
        if ($kstring) {
            ksort($arr);
        }
    }
    
    // 计算签名
    private function sign(string $appKey, string $appSecret, string $method, 
    int $timestamp, string $paramJson): string {
        $paramPattern = 'app_key' . $appKey . 'method' . $method . 'param_json' . $paramJson . 'timestamp' . $timestamp . 'v2';
        $signPattern = $appSecret . $paramPattern . $appSecret;
        return hash_hmac("sha256", $signPattern, $appSecret);
    }

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Listest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值