微信回复原理:
当普通微信用户向公众账号发送消息时,微信服务器首先收到用户发送的消息;
然后将用户信息和消息打包成xml格式的数据包,再将这个xml数据包通过post方法提交到开发者设置的url上。
疑问一:为何使用$globals[http_raw_post_data]保存post过来的数据,而非$_post数组?
回答:
post只能保存标准的数据类型,对于xml、soap或application/octet-steam之类的内容则无法解析。
而$globals[http_raw_post_data]和$_post是一样的,如果post过来的数据php能够识别,则可以用$globals[http_raw_post_data]来接收。
疑问二:simplexml_load_file()各参数和返回值是什么?
回答:
参数含义
string:需要处理的xml字符串。
class:用来指定新对象,通常设置为simplexmlelement,生成一个简单xml元素的类。
options:指定附加的libxml参数,通常设置为常量libxml_nocdata,表示把cdata设置为文本节点。
ns:一般省略
is_prefix:一般省略
函数执行完成后返回simplexmlelement类的一个对象。
功能:公众号只接受文字消息,且做出相应的文字回复。
valid(); class wechat{ public function valid(){ $echostr = $_get['echostr']; //如果是第一次接入 if($this->checksignature() && $echostr ){ echo $echostr; exit; }else{ $this->responsemsg(); } } //校验方法 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; } } /* 普通文本消息 1348831860 */ public function responsemsg(){ //获取微信服务器post请求中的数据 $poststr = $globals[http_raw_post_data]; if( !empty($poststr) ){ $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); $fromuser = $postobj->fromusername; $touser = $postobj->tousername; $keyword = trim($postobj->content); $time = time(); $template = %s; if( strtolower($postobj->msgtype)!='text' ){ $msgtype = text; $content = 我只接受文本消息; }else{ $msgtype = text; if( !empty($keyword) ){ $content = 您发送的消息是:.$postobj->content; }else{ $content = 请输入关键字;//消息为空 } } $info = sprintf($template, $fromuser, $touser, $time, $msgtype, $content); echo $info; }else{ echo ; exit; } } }
功能:公众号只接受图片消息,且做出相应的文字回复。
valid(); class wechat{ public function valid(){ $echostr = $_get['echostr']; //如果是第一次接入 if($this->checksignature() && $echostr ){ echo $echostr; exit; }else{ $this->responsemsg(); } } //校验方法 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; } } /* 接收图片消息格式 13488318601234567890123456 */ public function responsemsg(){ //获取微信服务器post请求中的数据 $poststr = $globals[http_raw_post_data]; if( !empty($poststr) ){ $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); $fromuser = $postobj->fromusername; $touser = $postobj->tousername; $time = time(); $msgtype= $postobj->msgtype; $picurl = $postobj->picurl; $mediaid = $postobj->mediaid; $template = %s; if( strtolower($msgtype)!='image' ){ $msgtype = text; $content = 我只接受图片消息; }else{ $msgtype = text; if( !empty( $picurl ) ){ $content = 图片链接为:.$picurl.\n; $content .= 媒体id:.$mediaid; }else{ $content = 请发送图片;//消息为空 } } $info = sprintf($template, $fromuser, $touser, $time, $msgtype, $content); echo $info; }else{ echo ; exit; } } }
以上是小编给大家分享的微信消息自动回复下所遇到的坑的相关知识,希望对大家有所帮助!
以上就介绍了php微信开发之微信消息自动回复下所遇到的坑,包括了微信开发,自动回复方面的内容,希望对php教程有兴趣的朋友有所帮助。