微信公用帳號開發模式PHP介面實現

來源:互聯網
上載者:User
<?phpdefine("TOKEN","你的TOKEN");class wechatCallBackApi{    //public $signature;    //加密簽名    //public $timestamp;    //時間戳記    //public $nonce;        //隨機數    //public $echostr;    //隨機字串,返回標誌    public $fromUserName;    //發信人ID    public $toUserName;        //收信人ID(本公用號)    public $keyWords;        //文本資訊    public $msgId;        //訊息ID    public $welcomeMsg; //歡迎訊息    public $msgType;    //訊息類型    public $event;        //事件名    public $mediaId;    //語音訊息媒體id,可以調用多媒體檔案下載介面拉取該媒體    public $format;        //語音格式:amr    public $recognition;    //語音辨識結果    public function __construct(){        $this->welcomeMsg="HELLO,歡迎訂閱!";            //$this->main();    //*****************第一次驗證的時候開啟,隨後可關閉***************    }    //首次TOKEN驗證    public function main(){        $echoStr=$_GET['echostr'];        if($this->checkRes()){            echo $echoStr;        }else{            exit();        }    }        //驗證許可權    private function checkRes(){        $signature=$_GET['signature'];    //擷取服務發送過來的參數        $timestamp=$_GET['timestamp'];        $nonce=$_GET['nonce'];        $token = TOKEN;        $tmpArr = array($token,$timestamp,$nonce);        sort($tmpArr);    //進行字典排序        $strWaitCheck = implode($tmpArr);    //進行字串組合        $strWaitCheck = sha1($strWaitCheck);    //對字串進行sha1加密        if($strWaitCheck==$signature){            return true;        }else{            return false;        }    }    public function getWhatWeChatServerPost(){            $huntPostContent = $GLOBALS["HTTP_RAW_POST_DATA"];    //擷取服務post過來的內容            $newXmlObj =simplexml_load_string($huntPostContent,'SimpleXMLElement', LIBXML_NOCDATA);    //把post過的字串載入對象            $this->fromUserName = $newXmlObj->FromUserName;    //向公用帳號發資訊的使用者名稱            $this->toUserName = $newXmlObj->ToUserName;    //公用帳號名            $this->keyWords = trim($newXmlObj->Content);    //訊息內容            $this->msgId = trim($newXmlObj->MsgId);        //讀取當前訊息msgId            $this->msgType = trim($newXmlObj->MsgType);        //讀取當前訊息類型            $this->event = trim($newXmlObj->Event);        //讀取當前事件名                        $this->format = trim($newXmlObj->Format);    //語音格式:amr                        $this->mediaId = trim($newXmlObj->MediaID);    //語音訊息媒體id,可以調用多媒體檔案下載介面拉取該媒體                        $this->recognition = trim($newXmlObj->Recognition);    //語音辨識結果,UTF8編碼        }    //訊息響應    public function responseMsg(){            $this->getWhatWeChatServerPost();    //執行接收伺服器POST過來的東西                                //當訊息類型為text的時候處理                    if(!empty($this->keyWords)&& $this->msgType == "text"){                            $para = $this->keyWords;                            switch($para){                                    case "1":                                    $tmp_item = $this->textPicMaxTmp(                                    "this is a test!!",                                    "各位大叔大媽別太大較真,這就一測試滴",                                    "http://www.baidu.com/img/bdlogo.gif",                                    "http://www.baidu.com/");                                    $re_global = $this->resTextPicMax($this->fromUserName,$this->toUserName,1,$tmp_item);                                    echo $re_global;                                    break;                                case "2":                                    $contentResText = "Hello,buddy,you send me a \"2\"[玫瑰]";                                    $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);                                    echo $re_global;                                    break;                                                                    case "000":                                        $contentResText = "你想返回的字串";                                    $this->longStrHandleExtendfunc($contentResText);                                    break;                                                  default:                                    //調用功能判斷函數處理                                    $this->longStrHandle($para);                                       break;                                }                        }else if($this->msgType == "event" && !empty($this->event)){        //擷取事件類型                                    if($this->event=='subscribe'){                                            $contentResText = $this->welcomeMsg;    //初次關注的時候推送歡迎訊息                                            $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);                                        }else if($this->event == "unsubscribe"){///這個地方好像沒有作用!!!                                            $contentResText = "我擦,你咋跑了";                                            $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);                                            }                            }else if($this->msgType == "voice"){                                        if($this->recognition == ""){                                                $contentResText ="親,你說的火星文麼,聽不懂,建議您還是文字查詢吧..[呲牙]";//語音無返回結果的時候給用記返回一條錯誤                                                $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);                                            }else{                                                $contentResText =$this->recognition; //其它情況下返回歡迎語                                                $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);                                                                                                    }                                                                    }else{                            //如果在text情況意外得到一條空訊息的處理                            echo "";                            exit();                            }                    echo $re_global;    //傳回值給伺服器        }                        //多次用到的幾句話就定義了一個小方法            public function longStrHandleExtendfunc($conText){                    $re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$conText);                    echo $re_global;                }                            //收到除特定字元外的其它字元的處理方法,這個要根據你需要的具體邏輯處理                public function longStrHandle($waitHandleStr){                    /*                        your code                    */                            }        //回複文本資訊方法        public function resTextMsg($tu,$fu,$textContent){                //簡訊模板                    $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>";                    $msgType = "text";    //訊息類型                    $time = time();        //時間戳記                    $resServerStr = sprintf($textTpl,$tu,$fu,$time,$msgType,$textContent);    //替換模板裡的內容                    return $resServerStr;            }        //回複圖文        public function resTextPicMax($tu,$fu,$itemNum,$totalItemStr){                //圖片模板                    $picTpl = "<xml>                                     <ToUserName><![CDATA[%s]]></ToUserName>                                     <FromUserName><![CDATA[%s]]></FromUserName>                                     <CreateTime>%s</CreateTime>                                     <MsgType><![CDATA[%s]]></MsgType>                                     <ArticleCount>%s</ArticleCount>                                     <Articles>"                                    .$totalItemStr.                                     "</Articles>                                 </xml>";                    $msgType = "news";    //訊息類型                    $time = time();        //時間戳記                    $resServerStr = sprintf($picTpl,$this->fromUserName,$this->toUserName,$time,$msgType,$itemNum);    //替換模板裡的內容                    return $resServerStr;            }            //圖文模板處理            public function textPicMaxTmp($title,$des,$picUrl,$jumpUrl){                    //圖文訊息條目模板                $itemTpl="                         <item>                                     <Title><![CDATA[%s]]></Title>                                     <Description><![CDATA[%s]]></Description>                                     <PicUrl><![CDATA[%s]]></PicUrl>                                     <Url><![CDATA[%s]]></Url>                                     </item>                ";                $resItemStr = sprintf($itemTpl,$title,$des,$picUrl,$jumpUrl);    //替換模板裡的內容                return $resItemStr;                }            //接下來就是你自己定義的各種方法                  }
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.