<?php/** * wechat php test */ //define your token//定義TOKEN秘鑰define("TOKEN", "weixin"); //執行個體化微信對象$wechatObj = new wechatCallbackapiTest();//驗證成功後注釋valid方法//$wechatObj->valid();//開啟自動回複功能$wechatObj->responseMsg(); //定義類檔案class wechatCallbackapiTest{ //實現valid驗證方法:實現對接微信公眾平台 public function valid() { //接受隨機字串 $echoStr = $_GET["echostr"]; //valid signature , option //進行使用者數位簽章驗證 if($this->checkSignature()){ //如果成功,則返回接受到的隨機字串 echo $echoStr; //退出 exit; } } //定義自動回複功能 public function responseMsg() { //get post data, May be due to the different environments //接受使用者端發送過來的xml資料 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data //判斷xml資料是否為空白 if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); //通過simplexml進行xml解析 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //接受微信的手機端 $fromUsername = $postObj->FromUserName; //微信公眾平台 $toUsername = $postObj->ToUserName; //接受使用者發送的關鍵詞 $keyword = trim($postObj->Content); //1.接受使用者訊息類型 $msgType = $postObj -> MsgType; //時間戳記 $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($msgType == 'text'){ if(!empty( $keyword )) { /*這是一個執行個體 //如果發送文本資訊 $msgType = "text"; //回複內容 if($keyword == "李楠"){ $contentStr = "叫我幹嘛"; }else{ $contentStr = "叫我幹嘛"; } //格式化xml模板,參數與上面的模板是一一對應的.fromUsername和頭Username是相反的,唯寫帶%s的 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; */ if($keyword == "?" || $keyword == "?"){ $msgType = "text"; $contentStr = "1.特種服務號碼\n2.通訊服務號碼"; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); echo $resultStr; }elseif($keyword == 1){ $msgType = "text"; $contentStr = "1.匪警:110\n2.火警:119\n3.急救:120"; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); echo $resultStr; }elseif($keyword == 2){ $msgType = "text"; $contentStr = "1.中國移動:10086\n2.中國聯通:10010"; $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr); echo $resultStr; } }else{ echo "不能不說話"; } } //////////////////////////////////////////////////////////////////////////////////// //接受圖片資訊 if($msgType == "image"){ //如果發送文本資訊 $msgType = "text"; //回複內容 $contentStr = "你發送的是圖片檔案"; //格式化字串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; } //////////////////////////////////////////////////////////////////////////////////// if($msgType == "voice"){ //如果發送文本資訊 $msgType = "text"; //回複內容 $contentStr = "你發送的是語音檔案"; //格式化字串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; } //////////////////////////////////////////////////////////////////////////////////// if($msgType == "video"){ //如果發送文本資訊 $msgType = "text"; //回複內容 $contentStr = "你發送的是視頻檔案"; //格式化字串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; } //////////////////////////////////////////////////////////////////////////////////// if($msgType == "shortvideo"){ //如果發送文本資訊 $msgType = "text"; //回複內容 $contentStr = "你發送的是小視頻檔案"; //格式化字串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; } //////////////////////////////////////////////////////////////////////////////////// if($msgType == "location"){ //如果發送文本資訊 $msgType = "text"; //回複內容 $contentStr = "你發送的是地理位置檔案"; //格式化字串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; } //////////////////////////////////////////////////////////////////////////////////// if($msgType == "link"){ //如果發送文本資訊 $msgType = "text"; //回複內容 $contentStr = "你發送的是串連檔案"; //格式化字串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; } //////////////////////////////////////////////////////////////////////////////////// /* //判斷使用者發送關鍵詞是否為空白 if(!empty( $keyword )) { //如果發送文本資訊 $msgType = "text"; //回複內容 $contentStr = "大家好,我是hero"; //格式化字串 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //將xml資訊返回給用戶端 echo $resultStr; }else{ echo "Input something..."; } */ }else { echo ""; exit; } } private function checkSignature() { // you must define TOKEN by yourself //判斷是否定義了TOKEN,如果沒有就拋出一個異常 if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"];//接受微信加密簽名 $timestamp = $_GET["timestamp"];//接受時間戳記 $nonce = $_GET["nonce"];//接受隨機數 $token = TOKEN;//把TOKEN常量賦值給$token //把相關參數組裝成數組 $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule //排序 sort($tmpArr, SORT_STRING); //把排序後的數群組轉換成字串 $tmpStr = implode( $tmpArr ); //通過雜湊演算法加密 $tmpStr = sha1( $tmpStr ); //與加密簽名進行對比 if( $tmpStr == $signature ){ //相同返回true return true; }else{ //不同返回false return false; } }} ?>