Home Backend Development PHP Tutorial thinkPHP WeChat sharing interface JSSDK example explanation

thinkPHP WeChat sharing interface JSSDK example explanation

May 18, 2018 pm 04:49 PM
javascript thinkphp

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缓存表';
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

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!

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

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

See all articles