微信公众号服务器配置

如图

image

在设置URL 时候,微信会给你的url以GET方式传入 signature timestamp nonce echostr几个值,和验证token处理数据返回的echostr

代码如下 (我添加了写入日志文件的 操作)

        $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
        $fp = fopen($DOCUMENT_ROOT.'/aaa.txt','a');
        fwrite($fp, '测试写入 - '.date('Y-m-d H:i:s')."\n");
         
        
        
        // 微信token认证
        $signature = $_GET["signature"] ?  $_GET["signature"] : '';
        $timestamp = $_GET["timestamp"] ?  $_GET["timestamp"] : '' ;
        $nonce = $_GET["nonce"]  ? $_GET["nonce"] : ''  ;
        $echostr = $_GET["echostr"] ? $_GET["echostr"] : ''  ;
        // 你在微信公众号后台的设置的Token
        
        fwrite($fp, '测试写入 - '.$signature."\n");
        fwrite($fp, '测试写入 - '.$timestamp."\n");
        fwrite($fp, '测试写入 - '.$nonce."\n");
        fwrite($fp, '测试写入 - '.$echostr."\n");
        
         
         
         
        $token = "ffsgdfsgfsadsadasd";
        
        // 1)将token、timestamp、nonce三个参数进行字典序排序
        $tmpArr = array($nonce, $token, $timestamp);
        sort($tmpArr, SORT_STRING);
        
        // 2)将三个参数字符串拼接成一个字符串进行sha1加密
        $str = implode($tmpArr);
        $sign = sha1($str);
        
        // 3)开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
        if ($sign == $signature) {
            // echo $echostr;
            return $echostr; 
        }
        

posted @ 2024-04-01 17:55  79524795  阅读(34)  评论(0编辑  收藏  举报