Home>Article>Backend Development> Usage analysis of thinkPHP WeChat sharing interface JSSDK

Usage analysis of thinkPHP WeChat sharing interface JSSDK

不言
不言 Original
2018-06-08 14:50:12 1693browse

This article mainly introduces the usage of thinkPHP WeChat sharing interface JSSDK, and analyzes the specific steps and related operating techniques of thinkPHP calling WeChat sharing interface in the form of examples. Friends in need can refer to the following

The examples in this article describe thinkPHP WeChat sharing interface JSSDK usage. Share it with everyone for your reference, the details are as follows:

First add the access_token table in the database:

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); } }

The above is the entire content of this article, I hope it will be helpful to everyone's study, more related Please pay attention to the PHP Chinese website for content!

Related recommendations:

About the analysis of the callback problem of the thinkPHP framework docking with Alipay’s instant payment interface

About thinkphp3.2 Analysis of embedding Baidu editor ueditor

How to use thinkphp to obtain the client IP

The above is the detailed content of Usage analysis of thinkPHP WeChat sharing interface JSSDK. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn