微信公众平台开发实战与应用案例读书笔记

Coding Alan 9年前 (2015-09-23) 12960次浏览 0个评论 扫描二维码

公众号回复消息代码实现

链接: http://pan.baidu.com/s/1eQFb5QU 密码: c57t

微信公众平台开发实战与应用案例读书笔记

<?php //填入微信后台中使用的TOKEN define("TOKEN", ""); $wechatObj = new wechatCallbackapiTest(); if(!isset($_GET['echostr'])){ $wechatObj->responseMsg();
	}else{
		$wechatObj->valid();
	}	
	//回调类
	class wechatCallbackapiTest{
		public $fromUsername='';
		public $toUsername='';
		public $time='';
		public function valid(){
			$echostr = $_GET['echostr'];
			if($this->checkSignature()){
				echo $echostr;
				exit;
			}
		}
		//官方验证方法
		private function checkSignature(){
			$signature = $_GET["signature"];
		    $timestamp = $_GET["timestamp"];
		    $nonce = $_GET["nonce"];	
        		
			$token = TOKEN;
			$tmpArr = array($token, $timestamp, $nonce);
			sort($tmpArr, SORT_STRING);
			$tmpStr = implode( $tmpArr );
			$tmpStr = sha1( $tmpStr );
			
			if( $tmpStr == $signature ){
				return true;
			}else{
				return false;
			}
		}
		//消息回复方法
		public function responseMsg(){
			$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA );
			$this->fromUsername = $postObj->FromUserName;
			$this->toUsername = $postObj->ToUserName;
			$type = $postObj->MsgType;
			$event=$postObj->Event;
			$Event_Key=$postObj->EventKey;
			$this->mid=$postObj->MediaId;
			$link=$postObj->Url;

			$latitude  = $postObj->Location_X;
			$longitude = $postObj->Location_Y;
            $this->keyword = trim($postObj->Content);
            $this->time = time();
            $this->find_type($type);
        }
        //判断用户发送的消息类型调用不同方法进行回复
        public function find_type($type){
        	switch($type){
        		case 'text':
            		$result=$this->fetch_text($this->keyword);
            		break;
            	case 'image':
	                $result=$this->fetch_image($this->mid);
	                break;
                case 'voice':
	                $result=$this->fetch_voice($this->mid);
	                break;
                case 'video':
	                $result=$this->fetch_video($this->mid);
	                break;
      
	            default:
	                $result=$this->fetch_default_text($this->keyword);
	                break;
        	}
        	echo $result;
        }

        //判断用户发送的文本内容
    	public function fetch_text($keyword){
	       switch ($keyword) {
	           	case '图片':
	             	$res=$this->fetch_image();
	              	break;
	            case '语音':
	             	$res=$this->fetch_voice();
	              	break;
	            case '视频':
	             	$res=$this->fetch_video();
	               	break;
	            case '音乐':
	            	$res=$this->fetch_music();
	              	break;
	            case '单图文':
	             	$res=$this->fetch_sigle_news();
	               	break;
	            case '多图文':
	             	$res=$this->fetch_muti_news();
	              	break;
	           
	           default:
	               $res=$this->fetch_default_text($keyword);
	               break;
	       	}
	      	return $res;
	    }

	    //回复文本消息
	    public function fetch_default_text($keyword){
	    	if($keyword=''){
	    		echo "";
	    	}else{
	    		$textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[text]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
				$contentStr="您发送的是文本消息,消息内容是:".$keyword;
				$resultStr = sprintf($textTpl, $this->fromUsername, $this->toUsername, $this->time, $contentStr);
            	return $resultStr;
	    	}
	    }

    	//回复图片消息
	    public function fetch_image($mid='O1roH6Z6lAglbpNycSRiSGAIX-0M5hPwrNMn24n6QHsTWBCI1s_smv3cHhrxN32X'){
	        $textTpl = "<xml>
					  	<ToUserName><![CDATA[%s]]></ToUserName>
					 	<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[image]]></MsgType>
						<Image>
						<MediaId><![CDATA[%s]]></MediaId>
						</Image>
						</xml>";

			$resultStr=sprintf($textTpl,$this->fromUsername,$this->toUsername,$this->time,$mid);
			return $resultStr;
	    }

	    //回复语音消息
	    public function fetch_voice($mid='q8GlXn_aIrRRQS2_3THhScuWhM-ZGR1e512eiyX7I_kAiJoRRtbTJvhQg9LWeuSR'){
	        $textTpl = '<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[voice]]></MsgType>
						<Voice>
						<MediaId><![CDATA[%s]]></MediaId>
						</Voice>
						</xml>';
			$resultStr=sprintf($textTpl,$this->fromUsername,$this->toUsername,$this->time,$mid);
			return $resultStr;
	    }

	    //回复视频消息
	    public function fetch_video($mid='OZ4dnt_nujz5_zRqlwYHUo_aF4zTqn8ub7X-dN5lXmOT0jmhPV2zjld5qc50ueOC'){
	        $textTpl = '<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA . ]></MsgType>
						<Video>
						<MediaId><![CDATA[%s]]></MediaId>
						<Title><![CDATA[%s]]></Title>
						<Description><![CDATA[%s]]></Description>
						</Video> 
						</xml>';
			$title="随手拍的小视频";
			$desc="随手拍书桌小视频,只有几秒钟,用来测试。视频文件不宜太大,微信上传视频有大小限制";
			$resultStr=sprintf($textTpl,$this->fromUsername,$this->toUsername,$this->time,$mid,$title,$desc);
			return $resultStr;
	    }

	    //回复音乐消息
	    public function fetch_music(){
	        $textTpl = '<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[music]]></MsgType>
						<Music>
						<Title><![CDATA[梦中的婚礼]]></Title>
						<Description><![CDATA[理查德.克里德曼,非常美妙的旋律,钢琴的至美境界]]></Description>
						<MusicUrl><![CDATA[%s]]></MusicUrl>
						<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
						<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
						</Music>
						</xml>';
			$music_url="http://alanhou.org/a.mp3";
			$hq_url="http://alanhou.org/a.mp3";
			$mid="mYBU_kavD71rqGKg34YV47YNNcHz88C47cL9vN5QkDXdI8L_zIyWsXNPmfwICLWE";
			$resultStr=sprintf($textTpl,$this->fromUsername,$this->toUsername,$this->time,$music_url,$hq_url,$mid);
			return $resultStr;
	    }


		//回复单图文消息;
	    public function fetch_sigle_news(){
	        $textTpl = '<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[news]]></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> ';
			$title="Test个人理财中心";
			$desc="提供保险,投资,理财咨询服务。你身边的投资理财专家。";
			$picurl="http://alanhou.org/wp-content/uploads/2015/09/wechat-development-notes.png";
			$url="http://alanhou.org/";
			$resultStr=sprintf($textTpl,$this->fromUsername,$this->toUsername,$this->time,$title,$desc,$picurl,$url);
			return $resultStr;
	    }

	    //回复多图文消息
	    public function fetch_muti_news(){
	        $textTpl = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[news]]></MsgType>
						<ArticleCount>5</ArticleCount>
						<Articles>
						<item>
						        <Title><![CDATA[欢迎您!点击进入Alan Hou的首页]]></Title>
						        <Description><![CDATA[描述]]></Description>
						        <PicUrl><![CDATA[http://alanhou.org/wp-content/uploads/2015/09/wechat-development-notes.png]]></PicUrl>
						        <Url><![CDATA[http://alanhou.org]]></Url>
						    </item>

						    <item>
						        <Title><![CDATA[个人介绍]]></Title>
						        <Description><![CDATA[个人介绍]]></Description>
						        <PicUrl><![CDATA[http://alanhou.org/wp-content/uploads/2015/09/wechat-development-notes.png]]></PicUrl>
						        <Url><![CDATA[http://alanhou.org/]]></Url>
						    </item>

						    <item>
						        <Title><![CDATA[诚聘英才]]></Title>
						        <Description><![CDATA[招聘]]></Description>
						        <PicUrl><![CDATA[http://alanhou.org/wp-content/uploads/2015/09/wechat-development-notes.png]]></PicUrl>
						        <Url><![CDATA[http://alanhou.org/]]></Url>
						    </item>

						    <item>
						        <Title><![CDATA[微图文]]></Title>
						        <Description><![CDATA[图文]]></Description>
						        <PicUrl><![CDATA[http://alanhou.org/wp-content/uploads/2015/09/wechat-development-notes.png]]></PicUrl>
						        <Url><![CDATA[http://alanhou.org/]]></Url>
						    </item>

						    <item>
						        <Title><![CDATA[联系我]]></Title>
						        <Description><![CDATA[联系我]]></Description>
						        <PicUrl><![CDATA[http://alanhou.org/wp-content/uploads/2015/09/wechat-development-notes.png]]></PicUrl>
						        <Url><![CDATA[http://alanhou.org/]]></Url>
						    </item>
						</Articles>
						</xml>";
			 $resultStr = sprintf($textTpl, $this->fromUsername, $this->toUsername,time());
			 return $resultStr;
	    } 		    
	}
?>
喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址