logo

通过第三方平台帮助公众号生成带参数的二维码


干净用朋友
干净用朋友 2022-09-15 14:20:54 13838
分类专栏: 资讯

第三方平台接口文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list & t=resource/res_list & verify=1 & id=open1453779503 & token= & lang=zh_CN

公众号接口文档:https://mp.weixin.qq.com/wiki?t=resource/res_main & id=mp1443433542

关键步骤是通过第三方平台获取access_token

下面是部分代码及步骤

1,

//获取微信第三方平台component_access_token

if(!function_exists('getComponentAccessToken')){
    function getComponentAccessToken(){
        $cachePath = './cache/componentAccessToken';
        $data = json_decode(file_get_contents($cachePath),true);
        if($data['expire_time'] < time()){
            $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
            $post = json_encode(array(
                'component_appid'           => WEIXIN_OPEN_APPID,
                'component_appsecret'       => WEIXIN_OPEN_APPSECRET,
                'component_verify_ticket'   => getComponentVerifyTicket(),
            ));
            $res = json_decode(httpPost($url,$post),true);
            if($res['component_access_token']){
                $data['component_access_token'] = $res['component_access_token'];
                $data['expire_time'] = time() + 7000;
                file_put_contents($cachePath,json_encode($data));
            }
        }
        return $data['component_access_token'];
    }
}
//刷新令牌
if(!function_exists('refreshToken')){
    function refreshToken($appid,$token){
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.getComponentAccessToken();
        $post = json_encode(array(
            'component_appid'   => WEIXIN_OPEN_APPID,
            'authorizer_appid'  => $appid,
            'authorizer_refresh_token'  => $token,
        ));
        $res = json_decode(httpPost($url,$post),true);
        return $res;
    }
}

3,

    $res = $this->db->query('SELECT accesstoken,expires,refreshtoken FROM ******* WHERE appid = "' . $this->appid . '" LIMIT 1')->row_array();
    if($res['refreshtoken']) {
        if ($res['expires'] < time()) {
            $res = refreshToken($this->appid, $res['refreshtoken']);
            if ($res['authorizer_access_token']) {
                $res['accesstoken'] = $res['authorizer_access_token'];
                $this->ci->db->update('sp_weixin_number', array(
                    'accesstoken' => $res['authorizer_access_token'],
                    'expires' => time() + 7000,
                ), array(
                    'appid' => $this->appid,
                ));
            }
        }
        return $res['accesstoken'];

4,

    $token = $this->api->token();
    $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$token;
    $post = '{"expire_seconds": 7200, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": "'.$_id.'"}}}';
    echo httpPost($url, $post);

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:http://www.xckfsq.com/news/show.html?id=60
赞同 0
评论 0 条
干净用朋友
粉丝 0 发表 9 + 关注 私信
上周热门
【广西信创】《2024年信息类产品框架协议采购(台式计算机)项目征集文件(征求意见稿)》  4054
展会邀请|中科信创与您相约2024中国(安徽)社会公共安全产品展览会  3616
明朝万达视频内容防泄漏引擎,为视频数据安全保驾护航  3454
2024 年全国大学生物联网设计竞赛(华为杯) 科技讲座  3334
技巧:Debian11下添加BHCexam.cls试卷文类的PDF手册  3320
培训通知丨第二期信创云计算工程师专项培训班开始招生啦!  3197
【活动资讯】2024全国高校走进信创企业研学活动在深召开  3076
企业数据安全防控升级保障!北信源UEBA系统打造全流程内部数据防泄露管理闭环  3053
招聘丨重庆公共运输职业学院招聘公告  3050
聚辰股份董事长陈作涛先生应邀参加中国战略管理学会第八届年会并做主题演讲  2988
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
我有15积分有什么用? 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8