Based on thinkPHP3.2, thinkphp3.2token is used to access and query the token value on WeChat.

Source: Internet
Author: User
Tags sha1 encryption

Based on thinkPHP3.2, thinkphp3.2token is used to access and query the token value.

This example describes how to access and query the token Value Based on thinkPHP3.2. We will share this with you for your reference. The details are as follows:

1. Configure the TOKEN, APPID, and APPSECRET values in the con. fig file.

2. Controller WeixinController code:

<? Php/*** parent class Controller * @ author Songle **/namespace Weixin \ Controller; use Think \ Controller; class WeixinController extends Controller {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 '); // obtain the four parameters of the Server GET request $ signature = I ('signature'); $ timestamp = I ('timestamp'); $ Nonce = I ('nonce '); $ echostr = I ('chostr'); if (! Empty ($ echostr )&&! Empty ($ signature )&&! Empty ($ nonce) {// defines an array and stores three parameters: timestamp, nonce, and token $ tempArr = array ($ nonce, $ timestamp, $ token); // sort ($ tempArr, SORT_STRING); // convert the array to a string $ tmpStr = implode ($ tempArr ); // perform the sha1 encryption algorithm $ tmpStr = sha1 ($ tmpStr); // determine whether 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_t Oken = "fill in the last token value"; // replace it with your own if (time ()> ($ this-> last_time + 7200 )) {// GET request url $ 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 (GET and POST are supported) 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); // simulate POST curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); // POST content} $ outopt = curl_exec ($ ch); curl_close ($ ch); $ outopt = json_decode ($ outopt, true); return $ outopt ;}}

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.