【微信第三方平台】获取component_access_token

  作者: thtomatic  分类: php笔记   评论: [ 2 ] 条  浏览: [ 2426 ] 次

代码中运用到两个自定义函数 文末会展示出来(TP5开发)

函数中会运用到数据库 文末也会展示出数据库结构


废话不多 直接上代

    protected $encodingAesKey = '';//创建平台时填写的公众号消息加解密Key
    protected $token = '';//创建平台时填写的公众号消息校验Token
    protected $appId = '';//公众号第三方平台AppID
    protected $appSecret = '';//公众号第三方平台appSecret
    protected $component_access_token;


//获取component_access_token
    protected function component_access_token()
    {
        $component_access_token = C('component_access_token',false);

        if(is_array($component_access_token) && !empty($component_access_token))
        {
            $time = $component_access_token['create_time'] +7000;
            if($time > time())
            {
                   $this->component_access_token = $component_access_token['v'];
                   return $component_access_token['v'];
            }
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
        $param ['component_appid'] = $this->appId;
        $param ['component_appsecret'] = $this->appSecret;
        $param ['component_verify_ticket'] = C('ComponentVerifyTicket');
        $post_data = json_encode($param);

        $data = $this->postJson($url, $post_data );

        F('component_access_token',$data['component_access_token']);
        return $this->component_access_token();

    }
运用到的函数 C 和 F



/**
 * 写入配置
 * @param $key
 * @param $value
 * @return int|string
 */
function F($key,$value)
{
    $data['v']= $value;
    $data['create_time'] = time();
    $where['k'] = $key;
     Db::table('issue_config')->where($where)->delete();
    $data['k'] = $key;
    $res = Db::table('issue_config')->insertGetId($data);
    return $res;
}

/**
 * 获取配置
 * @param $key
 * @return mixed
 */
function C($key,$flag = true)
{
    $where['k'] = $key;
    if($flag)
    {
        $res = Db::table('issue_config')->where($where)->value('v','');
    }else{
        $res = Db::table('issue_config')->where($where)->find();
    }
    return $res;
}


函数中用到的数据库:


CREATE TABLE `issue_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `k` varchar(255) DEFAULT NULL,
  `v` text,
  `create_time` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=181 DEFAULT CHARSET=utf8;



版权所有:《thtomatic》 => 《【微信第三方平台】获取component_access_token
本文地址:https://ask.mykeji.net/phpnotes/207.html
除非注明,文章均为 《简单记录》 原创,欢迎转载!转载请注明本文地址,谢谢。


评论:

小菜鸟
2019-07-04 15:53
你好,第一次弄微信第三方开发,第一步就很懵,不知道怎么去获取code,看了你的文档一目了然,我想问一下,你这里面的postJson方法是干嘛的?在哪来的?
thtomatic
2019-07-18 20:03
@小菜鸟:postJson这个方法是自己封装的 忘记放上去了 这个方法里就是用curl函数去给一个网址post过去一个Json字符串

发表评论:

    22.80ms