微信开发之自动回复消息

自动回复消息,就是把自动回复的信息,组成xml文件每一个回复都会对应着一个xml文件,并不是所有回复都使用的一个xml。下面分别是回复文本、图片、图文消息。在回复图片和图文中都需要上传图片,分别需要点用接口。

这里需要用到四个数据库:





回复文本消息

<xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName>< ![CDATA[fromUser] ]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[你好] ]></Content> </xml>
参数 是否必须 描述
ToUserName 接收方帐号(收到的OpenID)
FromUserName 开发者微信号
CreateTime 消息创建时间 (整型)
MsgType text
Content 回复的消息内容(换行:在content中能够换行,微信客户端就支持换行显示)

代码示例:

//自动回复文本
	public function addtext(){

		$content = I('post.content');
		$keyword = I('post.keyword');
		if(empty($content) || empty($keyword)){
			$this->ajaxReturn(array('msg'=>'必须输入关键字和回复内容'));
		}
		$mp = $this->mp;
		$data['content'] = $content;
		$model = M('mp_reply_text');
		$ret = $model->add($data);
		// echo $ret;
		// exit;
		if(isset($ret)){
			$data1['keyword'] = $keyword;
			$data1['mpid'] = $mp['id'];
			$data1['reply_id'] = $ret;
			$data1['type'] = 'text';
			M('mp_rule')->add($data1);
			$this->ajaxReturn(array('msg'=>'添加成功'));
		}
		else{
			$this->ajaxReturn(array('msg'=>$ret));
		}		
	}

回复图片消息

<xml><ToUserName>< ![CDATA[toUser] ]></ToUserName><FromUserName>< ![CDATA[fromUser] ]></FromUserName><CreateTime>12345678</CreateTime><MsgType>< ![CDATA[image] ]></MsgType><Image><MediaId>< ![CDATA[media_id] ]></MediaId></Image></xml>
参数 是否必须 说明
ToUserName 接收方帐号(收到的OpenID)
FromUserName 开发者微信号
CreateTime 消息创建时间 (整型)
MsgType image
MediaId 通过素材管理中的接口上传多媒体文件,得到的id。

代码示例:

//自动回复图片
	public function addimage(){

		$url = I('post.url');//图片在本地服务器上的路径

		//相对路径->绝对路径
		$file = realpath('.' . $url);
		$keyword = I('post.keyword');
		if(empty($url) || empty($keyword)){
			$this->ajaxReturn(array('status'=>0,'msg'=>'必须输入关键字和回复图片'));
		}
		$access_token = getAccess_token();
		include APP_PATH . 'LaneWeChat/lanewechat.php';

		//上传永久图片api
		$api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image";

		$data['media'] = Curl::addFile($file);
		$ret = Curl::callWebServer($api,$data,'post',true,false);
		
		if(isset($ret['media_id'])){
			$data['media_id'] = $ret['media_id'];
			$data['url'] = $ret['url'];
			$model = M('mp_reply_image');
			$r = $model->add($data);
			if($r){
				$mp = $this->mp;
				$data['mpid'] = $mp['id'];
				$data['type'] = 'image';
				$data['keyword'] = $keyword;
				$data['reply_id'] = $r;
				M('mp_rule')->add($data);
				$this->ajaxReturn(array('status'=>1,'msg'=>'添加成功'));
			}else{
				$this->ajaxReturn(array('msg'=>$ret));
			}
		}else{
			$this->ajaxReturn(array('msg'=>$ret));
		}	
	}

回复图文消息

<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>
参数 是否必须 说明
ToUserName 接收方帐号(收到的OpenID)
FromUserName 开发者微信号
CreateTime 消息创建时间 (整型)
MsgType news
ArticleCount 图文消息个数,限制为8条以内
Articles 多条图文消息信息,默认第一个item为大图,注意,如果图文数超过8,则将会无响应
Title 图文消息标题
Description 图文消息描述
PicUrl 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
Url 点击图文消息跳转链接

代码示例:

//自动回复图文
	public function addnews(){
		$keyword = I('post.keyword');
		$title = I('post.title');
		$picurl = I('post.url');
		$description = I('post.content');
		$url = I('post.content_source_url');

		//相对路径->绝对路径
		$file = realpath('.' . $picurl);

		if(empty($picurl) || empty($keyword) || empty($title) || empty($description) || empty($url)){
			$this->ajaxReturn(array('status'=>0,'msg'=>'请将信息输入完整'));
		}	

		$access_token = getAccess_token();
		include APP_PATH . 'LaneWeChat/lanewechat.php';

		//上传永久图片api
		$api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=image";

		$data['media'] = '@' . $file;
		$ret = Curl::callWebServer($api,$data,'post',true,false);
		if(isset($ret['media_id'])){
			$data['picurl'] = $ret['url'];
			$data['title'] = $title;
			$data['description'] = $description;
			$data['url'] = $url;
			$model = M('mp_reply_news');
			$r = $model->add($data);
			if($r){
				$mp = $this->mp;
				$arr['mpid'] = $mp['id'];
				$arr['keyword'] = $keyword;
				$arr['type'] = 'news';
				$arr['reply_id'] = $r;
				M('mp_rule')->add($arr);
				$this->ajaxReturn(array('status'=>1,'msg'=>'添加成功'));
			}else{
				$this->ajaxReturn(array('msg'=>$ret));
			}
		}else{
			$this->ajaxReturn(array('msg'=>$ret));
		}
		

	}


猜你喜欢

转载自blog.csdn.net/tang_tss/article/details/80097532