<?php // //最簡單的驗證方式 // echo $_GET["echostr"]; //驗證是否來自于微信 function checkWeixin(){ //微信會發送4個參數到我們的伺服器背景 簽名 時間戳 随機字元串 随機數 $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $echostr = $_GET["echostr"]; $token = "qilipingmgl"; // 1)将token、timestamp、nonce三個參數進行字典序排序 $tmpArr = array($nonce,$token,$timestamp); sort($tmpArr,SORT_STRING); // 2)将三個參數字元串拼接成一個字元串進行sha1加密 $str = implode($tmpArr); $sign = sha1($str); // 3)開發者獲得加密後的字元串可與signature對比,辨別該請求來源于微信 if ($sign == $signature) { echo $echostr; } } checkWeixin(); ?>
