PHP微信开发之微信消息自动回复下所遇到的坑_php实例

WBOY
Release: 2016-06-07 17:07:27
Original
1127 people have browsed it

微信回复原理:

当普通微信用户向公众账号发送消息时,微信服务器首先收到用户发送的消息;

然后将用户信息和消息打包成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类的一个对象。

功能:公众号只接受文字消息,且做出相应的文字回复。

<?php define("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; } } /* 普通文本消息    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; } } }
Copy after login

功能:公众号只接受图片消息,且做出相应的文字回复。

<?php define("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; } } /* 接收图片消息格式    1348831860    1234567890123456  */ 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; } } }
Copy after login

以上是小编给大家分享的微信消息自动回复下所遇到的坑的相关知识,希望对大家有所帮助!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!