1. 程式人生 > >微信公眾號開發伺服器驗證thinkphp5

微信公眾號開發伺服器驗證thinkphp5

public function index(){
        if(isset($_GET['echostr'])){
            $this->valid();//驗證是否第一次
        }else{
            $this->responseMsg();//回覆訊息方法
            $this->definedItem();//自定義選單
        }
    }
    public function valid(){
        $echoStr = $_GET["echostr"];
        if($this->checkSignature() && $echoStr){
            echo $echoStr;
            exit;
        }
    }
    //驗證伺服器
    private function checkSignature(){
        //獲取微信提交的引數
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = "luoangen";
        //微信提交引數放入陣列
        $tmpArr = array($token, $timestamp, $nonce);
       // ①將token、timestamp、nonce三個引數進行字典序排序
        sort($tmpArr, SORT_STRING);
        //②將三個引數字串拼接成一個字串進行sha1加密
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        //③開發者獲得加密後的字串可與signature對比
        if( $tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }