微信消息自动回复原理与数据接收差异分析
微信自动回复机制解析:
当普通用户向公众号发送消息时,微信平台服务器首先接收该请求;
随后将用户ID及消息内容封装为XML格式数据包,并通过HTTP POST方式推送至开发者配置的回调地址。
疑问一:为何使用$GLOBALS["HTTP_RAW_POST_DATA"]获取POST数据,而非$_POST?
解答:
$_POST仅能处理标准表单数据,对于XML、SOAP或二进制流等非标准格式无法直接解析。
$GLOBALS["HTTP_RAW_POST_DATA"]用于接收原始POST数据,若PHP能识别其类型则可直接读取。
疑问二: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;
}
}
/* 普通文本消息
toUser
fromUser
1348831860
text
this is a test
*/
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
%s
%s
%s
%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;
}
}
}
场景:公众号仅接收图片消息并返回对应文字提示。
<?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;
}
}
/* 接收图片消息格式
toUser
fromUser
1348831860
image
this is a url
media_id
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
%s
%s
%s
%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;
}
}
}
以上分享了微信消息自动开发中的常见陷阱,希望能帮助开发者避坑!