PHP-微信公眾平台開發-接收用戶輸入消息類型並響應



<?php // 該代碼塊用於接收用戶消息,根據用戶輸入的消息類型進行判斷,文本,圖片,視頻,位置,鏈接,語音等,並取得值,處理后給予響應。 // 接收用戶消息 // 微信公眾賬號接收到用戶的消息類型判斷 // define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); //用戶發送的消息類型判斷 switch ($RX_TYPE) { case "text": //文本消息 $result = $this->receiveText($postObj); break; case "image": //圖片消息 $result = $this->receiveImage($postObj); break; case "voice": //語音消息 $result = $this->receiveVoice($postObj); break; case "video": //視頻消息 $result = $this->receiveVideo($postObj); break; case "location"://位置消息 $result = $this->receiveLocation($postObj); break; case "link": //鏈接消息 $result = $this->receiveLink($postObj); break; default: $result = "unknow msg type: ".$RX_TYPE; break; } echo $result; }else { echo ""; exit; } } /* * 接收文本消息 */ private function receiveText($object) { $content = "你發送的是文本,內容為:".$object->Content; $result = $this->transmitText($object, $content); return $result; } /* * 接收圖片消息 */ private function receiveImage($object) { $content = "你發送的是圖片,地址為:".$object->PicUrl; $result = $this->transmitText($object, $content); return $result; } /* * 接收語音消息 */ private function receiveVoice($object) { $content = "你發送的是語音,媒體ID為:".$object->MediaId; $result = $this->transmitText($object, $content); return $result; } /* * 接收視頻消息 */ private function receiveVideo($object) { $content = "你發送的是視頻,媒體ID為:".$object->MediaId; $result = $this->transmitText($object, $content); return $result; } /* * 接收位置消息 */ private function receiveLocation($object) { $content = "你發送的是位置,緯度為:".$object->Location_X.";經度為:".$object->Location_Y.";縮放級別為:".$object->Scale.";位置為:".$object->Label; $result = $this->transmitText($object, $content); return $result; } /* * 接收鏈接消息 */ private function receiveLink($object) { $content = "你發送的是鏈接,標題為:".$object->Title.";內容為:".$object->Description.";鏈接地址為:".$object->Url; $result = $this->transmitText($object, $content); return $result; } /* * 回復文本消息 */ private function transmitText($object, $content) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } } ?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM