laravel 微信公众号的Token无效解决的方案

在这里插入图片描述
laravel 代码的编写

<?php

namespace App\Http\Controllers\Admin\Official;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

define('TOKEN', 'yunlive11');

class WxresponseMsg extends Controller
{
	protected $appid = 'wxecbfea5580fb894c';
    protected $secret = 'daabe60820d93278cd1988e796b16fa5';

    /**
     * 自定义方法,用于微信访问验证开发者身份
     *
     * @param Request $request
     * @return array|null|string
     */
    public function wxtoken(Request $request)
    {
        $signature = $request->input('signature');
        $timestamp = $request->input('timestamp');
        $nonce = $request->input('nonce');
        $echoStr = $request->input('echostr');
        if ($this->checkSignature($signature, $timestamp, $nonce)) {
        	ob_end_clean(); //必须清空缓冲区
            return $echoStr;
        } else {
            return 'Token verification failed.';
        }
    }
 
    /**
     * 微信官方提供的验签方法
     *
     * @param $signature
     * @param $timestamp
     * @param $nonce
     * @return bool
     */
    private function checkSignature($signature, $timestamp, $nonce)
    {
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
 
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

	 

	// 微信事件推送接收方法 判断用户是否关注了公众号: 
    public function WxresponseMsg(){
    	$postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : "";
    	$this->getdump($postStr);
    	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    	if(empty($postObj)){
    		// 当$postObj->Event == 'subscribe'(第一次关注时候)通过生成二维码的参数判断
	    	if($postObj->Event == 'subscribe'){
			// 关注
			     if(isset($postObj->EventKey) || isset($postObj->Ticket)){
			         // 扫描带参数二维码,未关注推送
			         $EventKey = $postObj->EventKey; //事件KEY值,qrscene_为前缀,后面为二维码的参数值
			         $Ticket   = $postObj->Ticket;   //二维码的ticket
			     }else{
			         // 普通关注
			     }
			}

    }

在这里插入图片描述 ob_end_clean(); //必须清空缓冲区这个只有在laravel里面应该要。其他不知
h这个创建的链接要post和get都可以请求
laravel修改方法
在这里插入图片描述这个文件加入即可

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值