自己的微信公众号开通很久了,很久都没动过,每年都是到腾讯发邮件告诉我一年没登录过了,要我登录。
最近又要我登录一次,索性就再来玩一玩微信公众号。
之前在开启开发者模式的时候,是直接在服务器上echo $_GET['echostr'];通过验证。
今天就想着把验签自己做一下,代码如下:
<?php
/**
URL参数 描述
signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp 时间戳
nonce 随机数
echostr 随机字符串
*/
$myfile = fopen("wx.txt", "w") or die("Unable to open file!");
$txt = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
fwrite($myfile, $txt);
fclose($myfile);
//'signature=5eb25082bada75ebf2d0067732acacccb6efae07&echostr=3422932780626997117×tamp=1560765245&nonce=925140347';
$get_timeStamp = $_GET['timestamp'];
$get_nonce = $_GET['nonce'];
$get_echostr = $_GET['echostr'];
$get_signature = $_GET['signature'];
$token = '123abc';
$arr = [$get_timeStamp,$get_nonce,$token];
sort($arr,SORT_STRING);
$my_signature = sha1(implode('',$arr));
if ($my_signature == $get_signature) {
echo $get_echostr;
}else{
die('匹配失败!');
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号