当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP微信开发之微信消息自动回复下所遇到的坑

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

2017年12月12日  | 移动技术网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类的一个对象。

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

<span style="font-family:courier new;font-size:14px;"><?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; 
} 
} 
/* 普通文本消息 
<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;"><?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; 
} 
} 
/* 接收图片消息格式 
<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>

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网