1.微信公众号自动回复文本:
首先我们应该增加reply_id以及内容(content)到数据表rh_mp_reply_text,然后根据reply_id查找表rh_mp_rule的信息关键字。然后我们在回复信息的那个方法里写入以下代码:
case 'text':
$reply=M('mp_reply_text')->find($reply_id);
if($reply){
$reply_text=$reply['content'];
}else{
$reply_text="出错了";
}
return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text);
break;
下面是将页面获取的内容写入数据表:
$model=M('mp_reply_text');
$content=I('post.content');
$arr['content']=$content;
$ret=$model->add($arr);
$m=M('mp_rule');
$mp=getCurrentMp();
$a=array(
'mpid'=>$mp['id'],
'keyword'=>I('post.keyword'),
'type'=>'text',
'reply_id'=>$ret
);
$m->add($a);
if($ret){
$this->ajaxReturn(array('status'=>1,'msg'=>'添加成功','url'=>U('replytext')));
}else{
$this->ajaxReturn(array('status'=>0,'msg'=>'添加失败'));
}
2.回复图片
首先我们要将图片上传到微信服务器上,我们需要调用接口https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image,这样他会给我们返回一个media_id,这样我们在写入数据表。
$keyword=I('post.keyword');
$media_id=I('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';
$url1="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
$file=realpath('.'. $url);//相对路径->绝对路径
$data['media']=Curl::addFile($file);
$ret=Curl::callWebServer($url1,$data,'post',1,0);
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=array(
'mpid'=>$mp['id'],
'type'=>'image',
'keyword'=>$keyword,
'reply_id'=>$reply_id
);
$ret=M('mp_rule')->add($arr);
if($ret){
$this->ajaxReturn(array('status'=>1,'msg'=>'添加成功','url'=>U('replyimage')));
}else{
$this->ajaxReturn(array('status'=>0,'msg'=>'添加失败'));
}
}
case 'image':
$reply=M('mp_reply_image')->find($reply_id);
if($reply){
$media_id=$reply['media_id'];
return ResponsePassive::image($request['fromusername'],$request['tousername'],$media_id);
}else{
$reply_text="出错了";
return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text);
}
break;
3.回复图文
$result=I('post.');
if(empty($result['keyword'])|| empty($result['url'])){
$this->ajaxReturn(array('status'=>0,'msg'=>'必须输入关键字和选择图片'));
exit;
}
if(empty($result['media_id'])){
$accessToken=getAccess_token();
include APP_PATH . 'LaneWeChat/lanewechat.php';
$url1="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
$file=realpath('.'. $result['url']);//相对路径->绝对路径
$data['media']=Curl::addFile($file);
$ret=Curl::callWebServer($url1,$data,'post',1,0);
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['title']=$result['title'];
$data['description']=$result['content'];
$data['picurl']=$url;
$data['url']=$result['content_source_url'];
$reply_id=M('mp_reply_news')->add($data);
$mp=getCurrentMp();
$arr=array(
'mpid'=>$mp['id'],
'type'=>'news',
'keyword'=>$result['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'=>0,'msg'=>'添加失败'));
}
}
case 'news':
$reply=M('mp_reply_news')->find($reply_id);
if($reply){
$item[]=ResponsePassive::newsItem($reply['title'],$reply['descrpition'],$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;