《二》關注訂閱號時發出問候語

一、先啓用第《一》篇時配置的 url 基本信息

這裏寫圖片描述

二、 調用responseMsg方法,添加回復功能:

<?php
//define your token
define("TOKEN", "你的自定義token");
$wechatObj = new wechatCallbackapiTest();
//這裏要調用《responseMsg》這個方法。
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature() //配置時驗證URL有效性
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }

        $signature = $_GET["signature"]; //微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。
        $timestamp = $_GET["timestamp"]; //時間戳
        $nonce = $_GET["nonce"];  //隨機數

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr,SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        //5.6 之前:$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]
        //5.6 之後:$postStr = file_get_contents('php://input')
        $postStr = file_get_contents('php://input');
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
            //用戶關注時給的提示。
            if($postObj->MsgType=='event'&&$postObj->Event=='subscribe'){
                $msgType = "text";
                $contentStr = '歡迎關注《CBB》,CBB讓你的美更加與衆不同。';
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }
            else
             if(!empty( $keyword ))
            {
                if($keyword == "?" || $keyword == "?")
                {
                    $msgType = "text";
                    $contentStr = date("Y-m-d H:i:s",time());
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }else{
                    $msgType = "text";
                    $contentStr = "Welcome to wechat world!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
                }
            }else{
                echo "Input something...";
            }
        }else{
            echo "";
            exit;
        }
    }
}

三、微信關注該訂閱號

  • 列表內容

  • 這裏寫圖片描述

結束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章