基於PHP的微信公衆平臺開發(TOKEN驗證,消息回覆)

微信公衆平臺開發 
實現步驟: 
第一步:填寫服務器配置 
登陸微信公衆平臺官網後,在公衆平臺後臺管理頁面 - 開發者中心頁,點擊「修改配置」按鈕,填寫服務器地址(URL)、Token和EncodingAESKey,其中URL是開發者用來接收微信消息和事件的接口URL。Token可由開發者能夠任意填寫,用做生成簽名(該Token會和接口URL中包含的Token進行比對,從而驗證安全性)。EncodingAESKey由開發者手動填寫或隨機生成,將用做消息體加解密密鑰。php

同時,開發者可選擇消息加解密方式:明文模式、兼容模式和安全模式。模式的選擇與服務器配置在提交後都會當即生效,請開發者謹慎填寫及選擇。加解密方式的默認狀態爲明文模式,選擇兼容模式和安全模式須要提早配置好相關加解密代碼,詳情請參考消息體簽名及加解密部分的文檔。api

第二步:驗證服務器地址的有效性 
開發者提交信息後,微信服務器將發送GET請求到填寫的服務器地址URL上,GET請求攜帶四個參數: 
signature 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。 
timestamp 時間戳 
nonce 隨機數 
echostr 隨機字符串 
開發者經過檢驗signature對請求進行校驗(下面有校驗方式)。若確認這次GET請求來自微信服務器,請原樣返回echostr參數內容,則接入生效,成爲開發者成功,不然接入失敗。 
加密/校驗流程以下: 
1. 將token、timestamp、nonce三個參數進行字典序排序 
2. 將三個參數字符串拼接成一個字符串進行sha1加密 
3. 開發者得到加密後的字符串可與signature對比,標識該請求來源於微信。 
驗證代碼:數組

//得到參數 signature nonce token timestamp echostr
    $nonce     = $_GET['nonce'];
    $token     = 'imooc';
    $timestamp = $_GET['timestamp'];
    $echostr   = $_GET['echostr'];
    $signature = $_GET['signature'];
    //造成數組,而後按字典序排序
    $array = array();
    $array = array($nonce, $timestamp, $token);
    sort($array);
    //拼接成字符串,sha1加密 ,而後與signature進行校驗
    $str = sha1( implode( $array ) );
    if( $str == $signature && $echostr ){
        //第一次接入weixin api接口的時候
        echo  $echostr;
        exit;
    }

利用修改配置中的提交驗證TOKEN是否驗證成功!安全

消息回覆服務器

 

<?php
    //得到參數 signature nonce token timestamp echostr
    $nonce     = $_GET['nonce'];
    $token     = 'imooc';
    $timestamp = $_GET['timestamp'];
    $echostr   = $_GET['echostr'];
    $signature = $_GET['signature'];
    //造成數組,而後按字典序排序
    $array = array();
    $array = array($nonce, $timestamp, $token);
    sort($array);
    //拼接成字符串,sha1加密 ,而後與signature進行校驗
    $str = sha1( implode( $array ) );
    if( $str == $signature && $echostr ){
        //第一次接入weixin api接口的時候
        echo  $echostr;
        exit;
    }else{

        //1.獲取到微信推送過來post數據(xml格式)
        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
        //2.處理消息類型,並設置回覆類型和內容
        /*<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>*/
        $postObj = simplexml_load_string( $postArr );
        //$postObj->ToUserName = '';
        //$postObj->FromUserName = '';
        //$postObj->CreateTime = '';
        //$postObj->MsgType = '';
        //$postObj->Event = '';
        // gh_e79a177814ed
        //判斷該數據包是不是訂閱的事件推送
        if( strtolower( $postObj->MsgType) == 'event'){
            //若是是關注 subscribe 事件
            if( strtolower($postObj->Event == 'subscribe') ){
                //回覆用戶消息(純文本格式)
                $toUser   = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
                $time     = time();
                $msgType  =  'text';
                $content  = '歡迎關注咱們的微信公衆帳號'.$postObj->FromUserName.'-'.$postObj->ToUserName;
                $template = "<xml>
                                <ToUserName><![CDATA[%s]]></ToUserName>
                                <FromUserName><![CDATA[%s]]></FromUserName>
                                <CreateTime>%s</CreateTime>
                                <MsgType><![CDATA[%s]]></MsgType>
                                <Content><![CDATA[%s]]></Content>
                                </xml>";
                $info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
                echo $info;
                /*<xml>
                <ToUserName><![CDATA[toUser]]></ToUserName>
                <FromUserName><![CDATA[fromUser]]></FromUserName>
                <CreateTime>12345678</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[你好]]></Content>
                </xml>*/


            }
        }

    }

轉自:http://blog.csdn.net/helencoder/article/details/49513969微信

相關文章
相關標籤/搜索