zoukankan      html  css  js  c++  java
  • 获取微信公众号或小程序的access_token

    <?php
    
    /**
     * User: Eden
     * Date: 2019/3/21
     * 共有内容
     */
    
    namespace CommonService;
    
    use VendorFuncHttp;
    
    class AccessTokenService extends CommonService
    {
        public function __construct()
        {
            parent::__construct();
        }
    
    
        /**
         * 获取access_token
         * 小程序的appid,secret
         * 或者
         * 公众号的appid,secret 公众号获取access_token需要配置ip白名单
         */ 
        public function get_access_token($app_id, $app_secret)
        {
            // 查询缓存中是否存在
            $key = "access_token_" . $app_id;
            $ttl = $this->red->ttl($key);
            if ($ttl == -2) { // 不存在
                // step1 获取
                $request_url = "https://api.weixin.qq.com/cgi-bin/token?";
                $request_url .= "grant_type=client_credential&appid=" . $app_id . "&secret=" . $app_secret;
                $data = json_decode(Http::doGet($request_url, 5), true);
    
                // step2 存储
                $this->red->setex($key, $data['expires_in'] - 1000, $data['access_token']);
                return $data['access_token'];
            } else {
                return $this->red->get($key);
            }
        }
    }
    
    
  • 相关阅读:
    学习进度条
    阅读《实例化需求》10-12章有感
    学习进度条
    阅读《实例化需求》7–9章有感
    学习进度条
    软件需求与分析课堂讨论一
    课程引言课后作业1
    MVC实例应用模式
    MVC模式介绍
    二十三种设计模式
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/12673733.html
Copyright © 2011-2022 走看看