Home >Backend Development >PHP Tutorial >thinkPHP3.2 method to implement WeChat access and query token value

thinkPHP3.2 method to implement WeChat access and query token value

不言
不言Original
2018-06-07 17:18:512004browse

This article mainly introduces the method of realizing WeChat access and querying token value based on thinkPHP3.2. It analyzes the specific implementation steps and related operation skills of thinkPHP integrating WeChat interface operation token value query in the form of examples. Friends who need it can Refer to the following

The example of this article describes the method of realizing WeChat access and querying token value based on thinkPHP3.2. Share it with everyone for your reference, the details are as follows:

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

2. Controller WeixinController code:

<?php
/**
 * 微信父类控制器
 * @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(&#39;TOKEN&#39;);
    $this->appid=C(&#39;APPID&#39;);
    $this->appsecret=C(&#39;APPSECRET&#39;);
    //获取微信服务器GET请求的4个参数
    $signature = I(&#39;signature&#39;);
    $timestamp = I(&#39;timestamp&#39;);
    $nonce = I(&#39;nonce&#39;);
    $echostr = I(&#39;echostr&#39;);
    if (! empty ( $echostr) && ! empty ( $signature ) && ! empty ($nonce )) {
      //定义一个数组,存储其中3个参数,分别是timestamp,nonce和token
      $tempArr = array($nonce,$timestamp,$token);
      //进行排序
      sort($tempArr,SORT_STRING);
      //将数组转换成字符串
      $tmpStr = implode($tempArr);
      //进行sha1加密算法
      $tmpStr = sha1($tmpStr);
      //判断请求是否来自微信服务器,对比$tmpStr和$signature
      if($tmpStr == $signature)
      {
        echo $echostr;
      }
      exit();
    }
  }
  /**
   * 获取tooken值
   */
  public function getTooken(){
    $this->last_time = 1448012924;
    $access_token = "填写上一次的token值"; //需要替换成自己的
    if(time() > ($this->last_time + 7200))
    {
      //GET请求的地址
      $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[&#39;access_token&#39;];
    }
    return $access_token;
  }
  //https请求(支持GET和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); //模拟POST
      curl_setopt($ch,CURLOPT_POSTFIELDS,$data); //POST内容
    }
    $outopt = curl_exec($ch);
    curl_close($ch);
    $outopt = json_decode($outopt,true);
    return $outopt;
  }
}

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How PHP uses token to prevent repeated submission of forms, token form

The above is the detailed content of thinkPHP3.2 method to implement WeChat access and query token value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn