开发者首先通过公众平台配置好接入地址url以及token令牌,后台方法具体实现代码如下:
public function recv(){
//1. 获得参数
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = 'XX'; //此处的token为公众平台下的token令牌,请保持一致
//2. 形成数组,按字典序排序
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING); // use SORT_STRING rule
//3. 拼接字符串及加密
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
//4. 比对,返回结果
if( $tmpStr == $signature && $echostr){
//第一次验证
echo $echostr;
die;
}else{
//非第一次,实现其他功能
$this -> xx();
}
}