微信公众号二次开发关键字回复图文

要实现这个功能首先要建2个表(mp__reply_rule,mp_reply_news)这两个表要互相连接,(如果主键是自动增长型 成功后返回值就是最新插入的值(返回的是主键值) 。例如mp_reply_news里添加文字内容成功之后返回的主键值是reply_id。则在mp_reply_rule表里也建一个reply_id,让mp_reply_news表里的reply_id赋值给mp_reply_rule表里的reply_id,就此联系起来。

在实现自动回复图文,需要获取图片在微信里的url,注意不要混乱url

在前台有两个隐藏域,在后台要调用,路径需注意

代码如下:

case 'news':
                    $reply=M('mp_reply_news')->find($reply_id);
                    if($reply){
                        $item[]=ResponsePassive::newsItem($reply['title'],$reply['description'],$reply['picurl'],$reply['url']);
                         return ResponsePassive::news($request['fromusername'],$request['tousername'],$item);
                    }else{
                        $reply_text='出错啦';
                        return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text);
                    }
                   break;

public function replyNews(){
		if(IS_GET){
			$this->display();
		}else{
			$keyword=I('post.keyword');//关键字
			// echo $keyword;
			// exit;
			$title=I('title');//图文标题
			$content=I('content');//图文内容
			// $media_id=I('media_id');
			$picurl=I('post.url');//图片在本地服务器上的路径
			$url=I('content_source_url');//跳转页面
			
				$accessToken=getAccess_token();//口令
				include APP_PATH . 'LaneWeChat/lanewechat.php';	
				$api = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
				$file=realpath('.'. $picurl);//相对路径->绝对路径

				$data['media']=Curl::addFile($file);			
				$ret=Curl::callWebServer($api,$data,'post',1,0);
				if(isset($ret['url'])){
					// $media_id=$ret['media_id'];
					$url=$ret['url'];
					// dump($url);
					// exit;
				}else{
					$ret['fail']='本地图片上传公众号失败';
					$this->ajaxReturn(array('status'=>1,'msg'=>$ret));
					exit;
				}
				$data['picurl']=$url;
				$data['description']=$content;//
				$data['title']=$title;//
				$data['url']=I('post.content_source_url');
				
				$reply_id=M('mp_reply_news')->add($data);

				$mp=getCurrentMp();//获取当前公众号
				$arr=array(
					'mp_id'=>$mp['id'],//
					'type'=>'news',
					'keyword'=>$keyword,
					'reply_id'=>$reply_id
					);
				$ret=M('mp_rule')->add($arr);
				if($ret){
						$this->ajaxReturn(array('status'=>1,'msg'=>'添加成功','url'=>U('replynews')));
				}else{
					$this->ajaxReturn(array('status'=>1,'msg'=>'添加失败'));
				}

			
		}


}

你可能感兴趣的:(微信公众号二次开发关键字回复图文)