当用户向微信发送一条消息后,微信将会被动回复数据库对应的文字信息。
首先我们需要新建两个数据库,mp_rlue,reply_text。
![](https://i-blog.csdnimg.cn/blog_migrate/66b0d79c67624e868cbe7f12a7e4089f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c816d89266af65167470c5f6a1090a89.png)
除了数据库之外还需要在控制器中写相应的方法replytext,代码如下:
<?php
//回复文本
public function replytext($type=''){
if (empty($type)) {
$type='image';
}
$keyword = I('post.keyword');
$content = I('post.content');
$data['content']=$content;
$ret = M('mp_reply_text')->add($data);
if ($ret) {
//获取当前使用的公众号的id
$mp = getCurrentMp();
$mpid = $mp['id'];
$arr['mpid']=$mpid;
$arr['reply_id'] = $ret;
$arr['keyword']=$keyword;
$arr['type']='text';
$result = M('mp_rule')->add($arr);
if ($result) {
$this->ajaxReturn(array('msg'=>'添加成功'));
}
}
}
?>
此外还需要把LaneWeChat中的core里面的wechatrequest.lib.php中的text方法做些改动
当用户发送关键字时,或被动回复相应的文本,若没有则会回复‘出错了’使用success.
<?php
public static function text(&$request){
$mpid = $_GET['id'];
$content = $request['content'];
$where['mpid'] = $mpid;
$where['keyword'] = $content;
$data = M('mp_rule')->where($where)->find();
if ($data) {
//发送关键字
$reply_id = $data['reply_id'];
$type = $data['type'];
switch ($type) {
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;
default:
return "success";
break;
}
}else{
return "success";
}
}
?>
这样微信号回复文本消息就成功啦。