php $echostr,PHP thinkphp6 echostr参数正常返回,微信公众号服务器配置一直token验证失败...

前景

昨晚在进行微信公众号的校验开发,echostr参数都能正常返回,可始终报token校验失败问题,网上一直找不到相关的错误指导,于是乎我来啦~~特此记录下,开发框架是 thinkPHP 6。

错误原因

echostr参数正常返回但是token验证失败的原因在于,response的content-type。

thinkphp 默认的字符串返回的 content-type是 text/html,但是微信那边接收的好像是text/plain;charset=utf-8,所以一直校验不过去。

最终源码记录

//微信公众号绑定域名校验

public function checkSignature()

{

$result = $this->validate($_GET, [

'signature' => 'require',

'timestamp' => 'require',

'nonce' => 'require',

'echostr' => 'require',

]);

if ($result) {

$signature = $_GET["signature"];

$timestamp = $_GET["timestamp"];

$nonce = $_GET["nonce"];

$echostr = $_GET['echostr'];

$token = Config::get('my.WX_CHECK_TOKEN');

Log::info('微信请求了校验文件:' . json_encode($_GET));

$tmpArr = array($token, $timestamp, $nonce);

sort($tmpArr, SORT_STRING);

$tmpStr = implode($tmpArr);

$tmpStr = sha1($tmpStr);

Log::info('校验字符串为:' . $tmpStr);

//test

if ($tmpStr == $signature) {

Log::info('校验成功返回:' . $echostr);

return response($_GET['echostr'])->header([

'Content-Type' => 'text/plain;charset=utf-8'

]);

} else {

Log::info('校验失败!');

return response('false')->header([

'Content-Type' => 'text/plain;charset=utf-8'

]);

}

} else {

return $result;

}

}

加油~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值