PHP微信公眾平台可以根據用戶發送的信息進行判斷,然後給出對應的回復,具有良好的交互性。下文將模擬簡單的回復功能,根據這個案例,開發者也可以基本理解微信交互的原理,進行更深層次的開發。
用戶發送過來的文本信息,我們可以提取關鍵字,通過簡單的 if...elseif...else... 實現。
關鍵代碼如下:
if($keyword=="你好"){ $contentStr = "hello"; }elseif($keyword=="蘇州"){ $contentStr = "上有天堂,下有蘇杭"; }else{ $contentStr = "感謝您關注【卓錦蘇州】 微信號:zhuojinsz"; }
如果用戶發送"你好",則回復"hello",如果用戶發送"蘇州",則回復"上有天堂,下有蘇杭",其他信息,則回復你的歡迎詞。
<?php /** * wechat php test */ //define your token define("TOKEN", "zhuojin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); //$wechatObj->valid(); class wechatCallbackapiTest { /*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 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch($RX_TYPE) { case "text": $resultStr = $this->handleText($postObj); break; case "event": $resultStr = $this->handleEvent($postObj); break; default: $resultStr = "Unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } } public function handleText($postObj) { $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(!empty( $keyword )) { $msgType = "text"; if($keyword=="你好"){ $contentStr = "hello"; }elseif($keyword=="蘇州"){ $contentStr = "上有天堂,下有蘇杭"; }else{ $contentStr = "感謝您關注【卓錦蘇州】 微信號:zhuojinsz"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } } public function handleEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "感謝您關注【卓錦蘇州】"."\n"."微信號:zhuojinsz"."\n"."卓越錦繡,名城蘇州,我們為您提供蘇州本地生活指南,蘇州相關信息查詢,做最好的蘇州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"."【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"."【4】 蘇州信息查詢,如輸入:蘇州觀前街"."\n"."更多內容,敬請期待..."; break; default : $contentStr = "Unknow Event: ".$object->Event; break; } $resultStr = $this->responseText($object, $contentStr); return $resultStr; } public function responseText($object, $content, $flag=0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; } 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; } } } ?>