驗證的代碼實現
public function index()
{
//將timestamp,nonce,token按字典序排序
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = 'weixin';
$signature = $_GET['signature'];
//將排序之後的三個參數拼接之後用sha1加密
$array = array($timestamp, $nonce, $token);
sort($array);
$tmpstr = implode('', $array);
$tmpstr = sha1($tmpstr);
//將加密後的字符串與sianature進行對比,判斷請求是否來自於微信
if ($tmpstr == $signature) {
echo $_GET['echostr'];
exit;
}
}
接口寫好之後,點擊提交,如果提示token驗證失敗,檢查代碼,確認代碼沒錯的話,可能是網絡等原因的問題,多點幾次就行