laravel 配置微信公众号(服务器配置 token)
发表时间:2020-10-8
发布人:葵宇科技
浏览次数:55
令牌token与代码token字段一直,域名访问用get
/**
* 自定义方法,用于微信访问验证开发者身份
*
* @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;
}
}