时间:2021-07-01 10:21:17 帮助过:3人阅读
1.关键词回复文本内容
首先我们需在LaneWeChat/core/aes/wechatrequest.lib.php下面的text()方法中需要进行一些修改,代码如下:
public static function text(&$request){ // $content = '收到文本消息'; // return ResponsePassive::text($request['fromusername'], $request['tousername'], $content); $mpid = $_GET['id']; $content = $request['content']; $where['mp_id'] = $mpid; $where['keyword'] = $content; $data = M('mp_reply_rule')->where($where)->find(); if ($data) { $reply_id = $data['reply_id']; $type = $data['type']; if ($type == "text") { $reply = M('mp_reply_text')->find($reply_id); $reply_text = $reply['content']; return ResponsePassive::text($request['fromusername'],$request['tousername'],$reply_text); }else if($type == "image"){ $reply = M('mp_reply_image')->find($reply_id); $media_id=$reply['media_id']; return ResponsePassive::image($request['fromusername'],$request['tousername'],$media_id); }else if($type == "news"){ $reply = M('mp_reply_news')->find($reply_id); $item[] = ResponsePassive::newsItem($reply['title'],$reply['descrpition'],$reply['picurl'],$reply['url']); return ResponsePassive::news($request['fromusername'],$request['tousername'],$item); } }else{ return 'success'; } }
其次我们开始写后台PHP代码
public function replytext(){ if(IS_GET){ $this->display(); }else{ $content=I('post.content'); $keyword=I('post.keyword'); $data['content']=$content; $reply_id=M('mp_reply_text')->add($data); if(isset($reply_id)){ $mp=getCurrentMp(); $data['mp_id']=$mp['id']; $data['keyword']=$keyword; $data['type']='text'; $data['reply_id']=$reply_id; // print_r($data); // exit; M('mp_reply_rule')->add($data); $this->ajaxReturn(array('msg'=>'上传成功')); }else{ $this->ajaxReturn(array('msg'=>'上传失败')); } } }
根据前台页面输入关键字以及回复内容就可以实现自动回复文本内容
2.根据关键词自动回复图片
根据LaneWeChat/core/aes/wechatrequest.lib.php中text()中的方法可以自动判断要回复的是那种类型 只需在php后台中获取其type,自动回复图片我们在PHP中写入如下代码:
public function replyimage(){ if(IS_GET){ $this->display(); }else{ $url=I('post.url');//图片在本地服务器上的路径 $file=realpath('.' .$url);// 相对路径换位结对路径 $accessToken=getAccess_token(); include APP_PATH .'LaneWeChat/lanewechat.php'; $url="https://api.weixin.qq.com/cgi-bin/material/add_material?accessaccessToken&type=image"; $data['media']='@' .$file; $ret=Curl::callWebServer($url,$data,'post',true,false); if(isset($ret['media_id'])){ $mp=getCurrentMp(); $data['url']=$url; $data['media_id']=$ret['media_id']; $reply_id=M('mp_reply_image')->add($data); $keyword=I('post.keyword'); if(isset($reply_id)){ $mp=getCurrentMp(); $data['mp_id']=$mp['id']; $data['keyword']=$keyword; $data['type']='image'; $data['reply_id']=$reply_id; M('mp_reply_rule')->add($data); $this->ajaxReturn(array('msg'=>'上传成功')); }else{ $this->ajaxReturn(array('msg'=>'上传失败')); } }else{ $this->ajaxReturn(array('msg'=>'上传失败')); } } }
但要注意我们在回复图片时应提前在数据表中存入一张要回复的图片,所以我们还需在PHP中写入upload()方法。
3.根据关键词回复图文消息
相当于将回复文本和图片的道理相同,只需稍作修改即可,获取有效的字段。
相关推荐:
PHP微信开发之文本自动回复
PHP微信开发之翻译功能
PHP微信开发之获取微信精选文章
以上就是PHP微信开发之自动回复的详细内容,更多请关注Gxl网其它相关文章!