<?php
/*
signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串
验证签名步骤
1、字典排序
2、拼接字符串并sha1加密
3、对比签名
*/
$signature=$_GET["signature"];
$timestamp=$_GET["timestamp"];
$nonce=$_GET["nonce"];
$echostr=$_GET["echostr"];
$token="自己在微信公众号填写的token";
//1、字典排序
$arr=array($token,$timestamp,$nonce);
sort($arr,SORT_STRING);
//2、拼接成字符串并sha1
$mySign=sha1(implode($arr));
//3、对比签名
if($mySign==$signature){
echo $echostr;
}else{
tokenLog($mySign);//如果验证失败,就写入日志,方便查找原因。
}
/*函数区begin*/
function tokenLog($contentCS){
$fileAddress="./tokenLog.txt";
$contentBL=$contenCS;
file_put_contents($fileAddress,$contentBL,FILE_APPEND);
}
/*函数区end*/
?>