thinkPHP3.2 realization of WeChat access and the method of querying token value

Source: Internet
Author: User
Tags sha1 sha1 encryption
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!

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.