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 who need it can refer to it. I hope it can help everyone.
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); } }
Related recommendations:
WeChat jssdk sharing function example tutorial
Use JSSDK to obtain the geographical location in the web page
The picture display problem obtained by the WeChat jssdk interface
The above is the detailed content of thinkPHP WeChat sharing interface JSSDK example explanation. For more information, please follow other related articles on the PHP Chinese website!