編寫自動回複,針對使用者不同訊息類型做出回複,程式怎麼調都有問題額。
問題:無論我發送什麼訊息,文本也好,圖片也好,語音也好,都只回複“我只接收圖片訊息”,也就是說程式似乎不執行
if(strtolower($msgType) =="image"){..............}這一判斷.
求各位大神看一下~~~~
GetImageMsg();//這個要調用自動回複訊息!!$wechatObj->valid();class wechatCallbackapiTest{//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>介面驗證>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ header('content-type:text');//在樣本代碼上添加了如此語句,因而介面接成功 echo $echoStr; exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $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 GetImageMsg()//自動回複訊息 { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)) { libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $msgType=$postObj->$MsgType; $picUrl=trim($postObj->PicUrl); $mediaId=trim($postObj->MediaId); $time = time(); $textTpl = " %s %s %s %s %s 0 "; if(strtolower($msgType) =="image") { if(!empty($picUrl)){ $msgType ="text"; $contentStr = "圖片連結:".$picUrl."\n"; $contentStr =$contentStr."媒體ID:".$mediaId; }else{ $contentStr = "請發送圖片哦"; } }else{ $msgType ="text"; $contentStr = "我只接收圖片訊息"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } else { echo ""; exit; } } }?>
回複討論(解決方案)
可能65行應該是$msgType=$postObj->MsgType;這個吧
多了個$???
可能65行應該是$msgType=$postObj->MsgType;這個吧
多了個$???
謝謝~~~~很有用!虧我搞了半天,原來是這個呀