微信开发1.基于Thinkphp3.2网站实现微信接入以及查询tooken值

14 篇文章 0 订阅
14 篇文章 29 订阅

1.在con.fig文件里面配置TOKEN,APPID,APPSECRET值


<?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('TOKEN');
$this->appid=C('APPID');
$this->appsecret=C('APPSECRET');

//获取微信服务器GET请求的4个参数
$signature = I('signature');
$timestamp = I('timestamp');
$nonce = I('nonce');
$echostr = I('echostr');
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['access_token'];
}
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;
}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值