本文实例讲述了thinkphp微信分享接口jssdk用法。分享给大家供大家参考,具体如下:
首先在数据库中添加access_token表:
set foreign_key_checks=0;-- ------------------------------ table structure for access_token-- ----------------------------drop table if exists `access_token`;create table `access_token` ( `id` int(11) not null auto_increment, `access_token` char(64) not null comment '令牌-唯一标识', `expires_time` varchar(64) default null comment '过期时间', `ticket` char(64) not null comment '临时票据', `ticket_expires_time` varchar(64) default null comment '过期的票据时间', primary key (`id`)) engine=innodb auto_increment=8 default charset=utf8 comment='token缓存表';
/*** 添加微信分享接口* 第一步:access token*/public function getaccesstoken(){ $appid = '你的appid'; //获取用户唯一凭证 $secret = '你的secret'; //用户唯一凭证密钥 $time = time()+7000; //当前时间+2小时等于过期时间 if (!$token) { $res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' .$appid.'&secret='. $secret); $res = json_decode($res, true); $token = $res['access_token']; $model = d('access_token'); //把获取的token存储到数据库中 if($token){ $data = array( 'access_token' => $token, 'expires_time' => $time ); $data = $model->add($data); //把获得的token存储到数据库中 } } return $token;}
/*** 添加微信分享接口* 第二步:用第一步拿到的access_token 采用http get方式请求获得jsapi_ticket*/public function getjsapiticket(){ $time = time()+7000; //当前时间+2小时等于过期时间 $map['ticket_expires_time'] = array('gt',time()); $res = d('access_token')->where('ticket_expires_time')->field('ticket')->find(); if($res){ $ticket = $res['ticket']; $result['result'] = $ticket; //没查询到符合条件的 jsonpreturn($result); } else{ $token = $this->getaccesstoken(); $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$token."&type=jsapi"); $res = json_decode($res, true); $ticket = $res['ticket']; // ticket不能频繁的访问接口来获取,在每次获取后,我们把它保存到数据库中。 $model = d('access_token'); //把获取的ticket存储到数据库中 if($ticket){ $data = array( 'access_token' => $token, 'expires_time' => $time, 'ticket' => $ticket, 'ticket_expires_time' => $time ); $data = $model->add($data); //把获得的token存储到数据库中 } $result['result'] = $ticket; //没查询到符合条件的 jsonpreturn($result); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
关于thinkphp框架对接支付宝即时到账接口回调问题的解析
关于thinkphp3.2嵌入百度编辑器ueditor的解析
如何利用thinkphp获取客户端ip
以上就是关于thinkphp微信分享接口jssdk的用法解析的详细内容。
