<?php
/*
*最近在学习微信公众平台开发,写一个类,里面包括对微信用户回复消息的时候,普通文本消息,
*单图文消息,多图文消息的回复。
*
*在这里,只是简单的实现各种函数方法,没有什么特殊的业务逻辑。详细的业务消息的发送逻辑,
*大家可以根据自己的需求改写。
*
* 谢谢大家,相互学习。有问题欢迎大家指出。-_-
*/
class wxtest{
//创建一个函数来验证signature
public function check_signature(){
//获得参数,signature,nonce,token,timestamp ,echostr
$nonce=$_GET['nonce'];
$timestamp=$_GET['timestamp'];
$token='index';//这个和你公众平台的token是一致的
$echostr=$_GET['echostr'];
$signature=$_GET['signature'];
//形成数组字典序排序
$arr=array($nonce,$timestamp,$token);
//字典序排序
sort($arr,SORT_STRING);
//转化成字符串并且sha1加密
$str=sha1(implode($arr));
//验证signature
if($str==$signature){
//如果signature验证成功之后,直接输入echostr
//echostr是一个随机字符串
echo $echostr;
exit;
}
}
//创建一个回复文本消息的函数方法
public function response_info(){
//获取微信发送过来的post数据(xml格式)
$post_info=$GLOBALS["HTTP_RAW_POST_DATA"];
if(!empty($post_info)){
//处理消息类型与回复消息
//把消息类型转化为一个对象的格式
$post_obj=simplexml_load_string($post_info,'SimpleXMLElement', LIBXML_NOCDATA);
//调用对象获取相应的属性
$touser=$post_obj->ToUserName;
$fromuser=$post_obj->FromUserName;
//利用strtolower把获取的“MsgType”转化为小写的字母
//$msgtype=strtolower($post_obj->MsgType);
//利用trim()方法清除输入内容前后的空格字符
$contentkey=trim($post_obj->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(!empty($contentkey) && $contentkey==1){
$content="你好,欢迎你";
$msgtype='text';
$info=sprintf($template,$fromuser,$touser,$time,$msgtype,$content);
echo $info;
}
}}
//创建一个函数回复单图文消息
public function response_simple(){
//获取微信发送过来的post数据(xml格式)
$post_info=$GLOBALS["HTTP_RAW_POST_DATA"];
if(!empty($post_info)){
//处理消息类型与回复消息
//把消息类型转化为一个对象的格式
$post_obj=simplexml_load_string($post_info,'SimpleXMLElement', LIBXML_NOCDATA);
$touser=$post_obj->ToUserName;
$fromuser=$post_obj->FromUserName;
//$msgtype=strtolower($post_obj->MsgType);
$contentkey=trim($post_obj->Content);
$time=time();
$msgtype="news";
$template="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
if (!empty($contentkey) && $contentkey=='2') {
$title="你好,朋友";
$des="欢迎你,到鑫鑫来了";
$pict_url="http://114.215.124.171/3.jpg";
$Url="http://www.baidu.com/";
$info=sprintf($template,$fromuser,$touser,$time,$msgtype,$title,$des,$pict_url,$Url);
echo $info;
}
} }
//创建一个回复多图文消息的函数
public function response_more(){
//获取微信发送过来的post数据(xml格式)
$post_info=$GLOBALS["HTTP_RAW_POST_DATA"];
if(!empty($post_info)){
//处理消息类型与回复消息
//把消息类型转化为一个对象的格式
$post_obj=simplexml_load_string($post_info,'SimpleXMLElement', LIBXML_NOCDATA);
$touser=$post_obj->ToUserName;
$fromuser=$post_obj->FromUserName;
//$msgtype=strtolower($post_obj->MsgType);
$contentkey=trim($post_obj->Content);
$time=time();
$msgtype="news";
//创建一个数组,用来存放不同的图文内容
$arr=array(array("title"=>"你好,朋友",
"des"=>"欢迎你到鑫鑫来了",
"pict_url"=>"http://114.215.124.171/3.jpg",
"Url"=>"http://www.baidu.com/"),
array("title"=>"你好,美女1号",
"des"=>"欢迎你,1号想你了",
"pict_url"=>"http://114.215.124.171/4.jpg",
"Url"=>"http://www.baidu.com/"),
array("title"=>"你好,美女2号",
"des"=>"欢迎你,2号想你了",
"pict_url"=>"http://114.215.124.171/5.jpg",
"Url"=>"http://www.baidu.com/")
);
//图文消息的数据的模板
//采用拼接的方法来设置返回数据的模板
$template="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
if(!empty($contentkey) && $contentkey=='3'){
//用foreach函数遍历图文
foreach ($arr as $value) {
$template.=" <item>
<Title><![CDATA[".$value['title']."]]></Title>
<Description><![CDATA[".$value['des']."]]></Description>
<PicUrl><![CDATA[".$value['pict_url']."]]></PicUrl>
<Url><![CDATA[".$value['Url']."]]></Url>
</item>";
}
}
$template.= "</Articles>
</xml> ";
$info=sprintf($template,$fromuser,$touser,$time,$msgtype);
echo $info; }
}
}
//创建对象调用方法
$wxtest=new wxtest();
//$wxtest->check_signature();
$wxtest->response_info();
$wxtest->response_simple();
$wxtest->response_more();
?>
微信开发中signature的验证,文本消息回复,单图文消息回复,多图文消息回复
最新推荐文章于 2021-03-27 09:18:00 发布