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