thinkphp WeChat Sharing Interface Jssdk example explanation

Source: Internet
Author: User
This paper mainly introduces thinkphp sharing interface JSSDK usage, combined with instance form to analyze the specific steps of thinkphp call sharing interface and related operation skills, the need for friends can refer to, hope to help everyone.

First add the Access_token table to the database:

SET foreign_key_checks=0;--------------------------------Table structure for Access_token------------------------- -----DROP TABLE IF EXISTS ' Access_token '; CREATE TABLE ' Access_token ' (' id ' int (one) not null auto_increment, ' Access_token ' char (UP) ' NOT null COMMENT ' token-uniquely identified ', ' E Xpires_time ' varchar ' DEFAULT NULL COMMENT ' expiry time ', ' ticket ' char (+) ' not NULL COMMENT ' temporary ticket ', ' Ticket_expires_time ' var char (+) default NULL COMMENT ' Expired ticket time ', PRIMARY KEY (' id ')) engine=innodb auto_increment=8 default Charset=utf8 comment= ' Token cache table ';
/*** Add sharing interface * First step: Access Token*/public function Getaccesstoken () {$appid = ' Your AppID '; Get the user unique voucher $secret = ' Your secret '; User Unique Credential Key $time = time () +7000; The current time + 2 hours equals the expiration time 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 ');      Store the acquired token in the database if ($token) {$data = array (' access_token ' + = $token, ' expires_time ' = = $time      ); $data = $model->add ($data); Store the obtained token in the database}} return $token;} 
/*** Add a shared interface * Second step: Take the first step to get the Access_token using HTTP GET method request to get Jsapi_ticket*/public function Getjsapiticket () {$time = time () + 7000;  The current time + 2 hours equals the expiration time $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;  Failed to find eligible 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 cannot access the interface frequently, and after each fetch, we save it to the database. $model = D (' Access_token ');       Store the acquired ticket in the database if ($ticket) {$data = array (' access_token ' = = $token, ' expires_time ' = = $time,      ' Ticket ' = $ticket, ' ticket_expires_time ' and $time); $data = $model->add ($data); Store the obtained token in the database} $result [' result '] = $ticket; No query to the characterConditions of Jsonpreturn ($result); }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.