Home Backend Development PHP Tutorial Usage analysis of thinkPHP WeChat sharing interface JSSDK

Usage analysis of thinkPHP WeChat sharing interface JSSDK

Jun 08, 2018 pm 02:50 PM
thinkphp WeChat

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缓存表';
Copy after login
/**
* 添加微信分享接口
* 第一步: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;
}
Copy after login
/**
* 添加微信分享接口
* 第二步:用第一步拿到的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);
  }
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
How to solve the problem of cURL error 77 when connecting to Elasticsearch 8 using ThinkPHP6 and elasticsearch-php clients? How to solve the problem of cURL error 77 when connecting to Elasticsearch 8 using ThinkPHP6 and elasticsearch-php clients? Mar 31, 2025 pm 11:36 PM

Using the ThinkPHP6 framework combined with elasticsearch-php client to operate Elasticsearch...

ThinkPHP6 routing: How to completely obtain URL parameters containing special characters such as Chinese? ThinkPHP6 routing: How to completely obtain URL parameters containing special characters such as Chinese? Apr 01, 2025 pm 02:51 PM

ThinkPHP6 routing parameters are processed in Chinese and complete acquisition. In the ThinkPHP6 framework, URL parameters containing special characters (such as Chinese and punctuation marks) are often processed...

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

What are the development tools for H5 and mini program? What are the development tools for H5 and mini program? Apr 06, 2025 am 09:54 AM

H5 development tools recommendations: VSCode, WebStorm, Atom, Brackets, Sublime Text; Mini Program Development Tools: WeChat Developer Tools, Alipay Mini Program Developer Tools, Baidu Smart Mini Program IDE, Toutiao Mini Program Developer Tools, Taro.

See all articles