微信公众平台被动回复用户消息

当用户发送消息给公众号时(或某些特定的用户操作引发的事件推送时),会产生一个POST请求,开发者可以在响应包(Get)中返回特定XML结构,来对该消息进行响应(现支持回复文本、图片、图文、语音、视频、音乐)。严格来说,发送被动响应消息其实并不是一种接口,而是对微信服务器发过来消息的一次回复。

首先我们应该先了解回复消息的各类xml结构

回复文本消息


<xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName>< ![CDATA[fromUser] ]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[你好] ]></Content> </xml>

回复图片消息

<xml><ToUserName>< ![CDATA[toUser] ]></ToUserName><FromUserName>< ![CDATA[fromUser] ]></FromUserName><CreateTime>12345678</CreateTime><MsgType>< ![CDATA[image] ]></MsgType><Image><MediaId>< ![CDATA[media_id] ]></MediaId></Image></xml>

回复图文消息

<xml><ToUserName>< ![CDATA[toUser] ]></ToUserName><FromUserName>< ![CDATA[fromUser] ]></FromUserName><CreateTime>12345678</CreateTime><MsgType>< ![CDATA[news] ]></MsgType><ArticleCount>2</ArticleCount><Articles><item><Title>< ![CDATA[title1] ]></Title> <Description>< ![CDATA[description1] ]></Description><PicUrl>< ![CDATA[picurl] ]></PicUrl><Url>< ![CDATA[url] ]></Url></item><item><Title>< ![CDATA[title] ]></Title><Description>< ![CDATA[description] ]></Description><PicUrl>< ![CDATA[picurl] ]></PicUrl><Url>< ![CDATA[url] ]></Url></item></Articles></xml>

首先我们先把获取到的信息存入数据库然后把信息上传到微信平台用xml的形式我们这里引用了LaneWeChat框架

回复文字

public function keyword(){
		$mp=$this->mp;
		$mp_id=$mp['id'];
		$text['content']=I('post.content');
		$reply_id=M('mp_reply_text')->add($text);
		$key['reply_id']=$reply_id;
		$key['keyword']=I('post.keyword');
		$key['mpid']=$mp_id;
		$key['type']='text';
		M('mp_rule')->add($key);
	}

回复图片

public function replyimage(){
		if(IS_GET){
			$this->display();
			exit;
		}
		// $this->ajaxReturn(array('status'=>1,'msg'=>'添加成功'));
		// exit;

		$keyword=I('post.keyword');
		$media_id=I('post.media_id');
		$url=I('post.url');
		// if(empty($keyword)||empty($url)){
		// 	$this->ajaxReturn(array('status'=>0,'msg'=>'必须输入关键字和选择的图片'));
		// 	exit;
		// }
		//选择本地图片需上传到微信平台
		if(empty($media_id)){
			$accessToken=getAccess_token();
		    include APP_PATH . "LaneWeChat/lanewechat.php";

			//上传永久图片接口
			$file=realpath('.'.$url);
			// echo 11122;
			// exit;
			$api="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
			$data['media']='@' . $file;
			$ret=Curl::callWebServer($api,$data,'post',1,0);
			// $this->ajaxReturn($ret);
			// exit;

			// 上传成功
			if(isset($ret['media_id'])){
				$media_id=$ret['media_id'];
				$url=$ret['url'];
			}else{
				$ret['fail']='本地图片上传公众平台失败';
				$this->ajaxReturn(array('status'=>1,'msg'=>$ret));
				exit;
			}
		}

		$data['media_id']=$media_id;
		$data['url']=$url;
		$reply_id=M('mp_reply_image')->add($data);
		$mp=getCurrentMp();
		$arr['mpid']=$mp['id'];
		$arr['reply_id']=$reply_id;
		$arr['type']='image';
		$arr['keyword']=$keyword;
		$ret=M('mp_rule')->add($arr);
		if($ret){
			$this->ajaxReturn(array('status'=>1,'msg'=>'添加成功'));
		}else{
			$this->ajaxReturn(array('status'=>1,'msg'=>'添加失败'));
		}


	}

回复图文

public function keynews(){
		$mp=$this->mp;
		$mp_id=$mp['id'];
		$content = I('post.content');

		$media_id=I('post.media_id');
		$url=I('post.url');
		// if(empty($keyword)||empty($url)){
		// 	$this->ajaxReturn(array('status'=>0,'msg'=>'必须输入关键字和选择的图片'));
		// 	exit;
		// }
		//选择本地图片需上传到微信平台
		if(empty($media_id)){
			$accessToken=getAccess_token();
		    include APP_PATH . "LaneWeChat/lanewechat.php";

			//上传永久图片接口
			$file=realpath('.'.$url);
			// echo 11122;
			// exit;
			$api="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
			$data['media']='@' . $file;
			$ret=Curl::callWebServer($api,$data,'post',1,0);
			// $this->ajaxReturn($ret);
			// exit;

			// 上传成功
			if(isset($ret['media_id'])){
				$media_id=$ret['media_id'];
				$url=$ret['url'];
			}else{
				$ret['fail']='本地图片上传公众平台失败';
				$this->ajaxReturn(array('status'=>1,'msg'=>$ret));
				exit;
			}
		}



		$text['title']=I('post.title');
		$text['description']=I('post.content');
		$text['url']=I('post.content_source_url');
		$keyword=I('post.keyword');
		$text['picurl']=$url;
		$reply_id=M('mp_reply_news')->add($text);
		$key['reply_id']=$reply_id;
		$key['keyword']=I('post.keyword');
		$key['mpid']=$mp_id;
		$key['type']='news';
		M('mp_rule')->add($key);
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值