This article introduces the content of the PHP development of the automatic reply, has a certain reference value, now share to everyone, the need for friends can refer to
1. keyword Reply text content
First we need to make some changes in the text () method below lanewechat/core/aes/wechatrequest.lib.php, the code is as follows:
public static function text (& $request) {//$content = ' Receive text message '; 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 '; } }
Next we begin to write the background PHP code
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 (' msg ' = ' upload succeeded '); } else{ $this->ajaxreturn (Array (' msg ' = ' upload Failed ')}} }
automatically reply to text content by entering keywords and replying content according to the foreground page
2. Automatically reply to a picture by keyword
Depending on the method in text () in lanewechat/core/aes/wechatrequest.lib.php, you can automatically determine which type to reply to by simply retrieving its type in the background of PHP and automatically replying to the picture we write the following code in PHP:
Public Function Replyimage () {if (is_get) {$this->display (); }else{$url =i (' Post.url ');//The path of the picture on the local server $file =realpath ('. '. $url);//relative path transposition pair path $accessToken =getaccess_t Oken (); 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 ' = ' upload succeeded ')); }else{$this->ajaxreturn (Array (' msg ' = ' upload failed ')); }}else{$this->ajaxreturn (Array (' msg ' = ' upload failed ')); } } }
Note, however, that when replying to a picture, we should put a picture in the datasheet that we want to reply to in advance, so we also need to write the upload () method in PHP.
3. Reply to text message by keyword
The equivalent of the reply text and the image of the same reason, only a little modification, to obtain a valid field.