Home  >  Article  >  Backend Development  >  微信开发自动回复功能,消息类型判断失败,PHP语言

微信开发自动回复功能,消息类型判断失败,PHP语言

WBOY
WBOYOriginal
2016-06-20 12:38:201234browse

编写自动回复,针对用户不同消息类型做出回复,程序怎么调都有问题额。
问题:无论我发送什么消息,文本也好,图片也好,语音也好,都只回复“我只接收图片消息”,也就是说程序似乎不执行
 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                                                                                    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;这个吧
多了个$???

谢谢~~~~很有用!亏我搞了半天,原来是这个呀
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn