微信关注回复消息

<?php 
	define('TOKEN','yida');
	$obj=new Weixin();

	if(!isset($_GET['echostr'])){

		$obj->receive();

	}else{
		$obj->checkSignature();
	}
	

class   Weixin{

		


	public  function checkSignature()
	    {
		        $signature = $_GET["signature"];   //加密签名
		        $timestamp = $_GET["timestamp"]; //时间戳
		        $nonce = $_GET["nonce"];	//随机数
		        		
			$token = TOKEN; //token


			$tmpArr = array($token, $timestamp, $nonce);//组成新数组
			sort($tmpArr, SORT_STRING);//重新排序
			$tmpStr = implode( $tmpArr );//转换成字符串
			$tmpStr = sha1( $tmpStr );  //再将字符串进行加密
			

			if( $tmpStr == $signature ){

				echo  $_GET['echostr'];
			}else{
				return false;
			}
		 }


    public  function  receive(){
    	
    	$obj=$GLOBALS['HTTP_RAW_POST_DATA'];
    	$postSql=simplexml_load_string($obj,'SimpleXMLElement',LIBXML_NOCDATA);


    	$this->logger("接受:\n".$obj);

    	if(!empty($postSql)){

    		switch(trim($postSql->MsgType)){

    			case "text" :
    			$result=$this->receiveText($postSql);
    			break;
    			case "event" ://事件
    			$result=$this->receiveEvent($postSql);
    			break;

    			
    		}

    		if(!empty($result)){
    				echo $result;

    			}else{

    					$xml="<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<Content><![CDATA[%s]]></Content>
					  </xml>";
			   

				echo $result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),$postSql->MsgType,"没有这条文本消息");

    			}


    		
    	}



    }

    //自动回复文本
    private function receiveEvent($postSql){
    	$xml="<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<Content><![CDATA[%s]]></Content>
					</xml>";
			  

				$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),"text","欢迎听益达讲php开发微信公众账号");
				$this->logger("自动回复:\n".$result);
				return $result;
    }


    //处理文本消息
    private function receiveText($postSql){
    		$content=trim($postSql->Content);


    		if(strstr($content,"你好")){
    			$xml="<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<Content><![CDATA[%s]]></Content>
					  </xml>";
			  

				$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),$postSql->MsgType,"hello");

				

    		}else if(strstr($content,"单图文")){
    			$result=$this->receiveImage($postSql);
    		}else if(strstr($content,"多图文")){
    			$result=$this->receiveImages($postSql);
    		}else if(strstr($content,"图片")){
    			$result=$this->receiveMedia($postSql);
    		}

    		$this->logger("发送图文消息:\n".$result);


    		return $result;

    		

    }

    //处理图片消息
    private function receiveMedia($postSql){
    	$xml="<xml>
		<ToUserName><![CDATA[%s]]></ToUserName>
		<FromUserName><![CDATA[%s]]></FromUserName>
		<CreateTime>%s</CreateTime>
		<MsgType><![CDATA[%s]]></MsgType>
		<Image>
		<MediaId><![CDATA[%s]]></MediaId>
		</Image>
		</xml>";

			$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),"image","82-482CHnSTGSJMvrdz9WXHxP_Vf31PKeWFo6pRdMBIkCIO3QCaSwP60L7IbuNxB");

					return $result;


    }
    //处理单图文消息
    private function receiveImage($postSql){

    	$xml="<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> ";

			$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),"news","跟益达学微信开发,教益达泡妹子","它就是
	中国海南海花岛——缤纷水上乐园
	23项游乐
	打造雪山滑道区、激流河道区
	巨浪演绎区、阖家欢乐区
	极限滑道区五大主题游乐区","http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");

					return $result;



    }
    //处理多图文消息
    private function   receiveImages($postSql){
    			$content=array();
    			$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");
				$content[]=array("Title"=>"跟益达学微信开发,教益达泡妹子","Description"=>"它就是
				中国海南海花岛——缤纷水上乐园
				23项游乐
				打造雪山滑道区、激流河道区
				巨浪演绎区、阖家欢乐区
				极限滑道区五大主题游乐区","PicUrl"=>"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg","http://www.maiziedu.com/");


    			$str="<item>
				<Title><![CDATA[%s]]></Title>
				<Description><![CDATA[%s]]></Description>
				<PicUrl><![CDATA[%s]]></PicUrl>
				<Url><![CDATA[%s]]></Url>
				</item>";

				$news="";
				foreach ($content as $newArray) {
					$news.=sprintf($str,$newArray['Title'],$newArray['Description'],$newArray['PicUrl'],$newArray['Url']);
				}

    			$xml="<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType><![CDATA[%s]]></MsgType>
				<ArticleCount>%s</ArticleCount>
				<Articles>
					$news
				</Articles>
				</xml> ";

					$result=sprintf($xml,$postSql->FromUserName,$postSql->ToUserName,time(),"news",count($content));

					return $result;


    }


    //写日志
    private function logger($content){
    	$logSize=100000;

    	$log="log.txt";

    	if(file_exists($log) && filesize($log)  > $logSize){
    		unlink($log);
    	}

    	file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND);

    }

}


	



 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值