微信开发中signature的验证,文本消息回复,单图文消息回复,多图文消息回复

<?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();
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值