无为清净楼资源网 Design By www.qnjia.com
微信回复原理:
当普通微信用户向公众账号发送消息时,微信服务器首先收到用户发送的消息;
然后将用户信息和消息打包成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类的一个对象。
功能:公众号只接受文字消息,且做出相应的文字回复。
<span style="font-family:Courier New;font-size:14px;"><"TOKEN","weixin"); $weixinObj = new Wechat(); $weixinObj->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; } } /* 普通文本消息 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> </xml> */ 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 = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; 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; } } }</span>
功能:公众号只接受图片消息,且做出相应的文字回复。
<span style="font-family:Courier New;font-size:14px;"><"TOKEN","weixin"); $weixinObj = new Wechat(); $weixinObj->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; } } /* 接收图片消息格式 <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[image]]></MsgType> <PicUrl><![CDATA[this is a url]]></PicUrl> <MediaId><![CDATA[media_id]]></MediaId> <MsgId>1234567890123456</MsgId> </xml> */ 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 = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; 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; } } }</span>
以上是小编给大家分享的微信消息自动回复下所遇到的坑的相关知识,希望对大家有所帮助!
标签:
微信消息自动回复
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 黄心懋1991-轻忧郁【滚石】【WAV+CUE】
- 群星.2000-大地金曲世纪回顾2CD【大地】【WAV+CUE】
- 王菲.1996-浮躁(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 群星.2022-传家电视剧原声带【东阳欢娱】【FLAC分轨】
- 《摇滚教父 伍佰黄金精选 2CD》 [WAV+CUE][1GB]
- 《凤凰传奇 我从草原来》[WAV/分轨][400MB]
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】