PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

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

原创
2016-06-20 12:38:20 1095浏览

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

谢谢~~~~很有用!亏我搞了半天,原来是这个呀
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。