This article mainly introduces the method of realizing access and querying Tooken value based on thinkPHP3.2, and analyzes the specific implementation steps and related operation skills of thinkphp integrated interface operation token value query, and the necessary friends can refer to the following
This paper describes the method of realizing access and querying token value based on thinkPHP3.2. Share to everyone for your reference, as follows:
1. Configure the Token,appid,appsecret value in the Con.fig file
2. Controller Weixincontroller Code:
<?php/** * Parent Class Controller * @author Songle * */namespace weixin\controller;use think\controller;class Weixincontroller extends C Ontroller {private $last _time=null; Private $appid =null; Private $appsecret =null; function __construct () {parent::__construct (); $token =c (' token '); $this->appid=c (' AppID '); $this->appsecret=c (' Appsecret '); Gets the 4 parameters of a server GET request $signature = I (' signature '); $timestamp = I (' timestamp '); $nonce = I (' nonce '); $echostr = I (' echostr '); if (! empty ($ECHOSTR) &&! empty ($signature) &&! empty ($nonce)) {///define an array that stores 3 parameters, respectively Timest Amp,nonce and Token $tempArr = Array ($nonce, $timestamp, $token); Sort by ($TEMPARR, sort_string); Converts an array to a string $tmpStr = Implode ($TEMPARR); The SHA1 encryption Algorithm $TMPSTR = SHA1 ($TMPSTR); Determine if the request is from the server, compare $tmpstr and $signature if ($tmpStr = = $signature) {echo $echostr; } exit (); }}/** * Get tooken value */Public function Gettooken() {$this->last_time = 1448012924; $access _token = "Fill in the last token value"; Need to replace your own if (Time () > ($this->last_time + 7200)) {//get The requested address $url = "https://api.weixin.qq.com/cgi -bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret} "; $access _token_arr = $this->https_request ($url); $this->last_time = time (); return $access _token_arr[' Access_token ']; } return $access _token; }//https request (support get and post) public function https_request ($url, $data = null) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, curlopt_returntransfer,1); if (!empty ($data)) {curl_setopt ($ch, curlopt_post,1);//Analog POST curl_setopt ($ch, Curlopt_postfields, $data);//po St Content} $outopt = Curl_exec ($ch); Curl_close ($ch); $outopt = Json_decode ($outopt, true); return $outopt; }}
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!