Home > Backend Development > PHP Tutorial > PHP WeChat development automatic reply

PHP WeChat development automatic reply

不言
Release: 2023-03-24 11:22:02
Original
4532 people have browsed it

The content of this article is about the automatic reply of PHP WeChat development. It has a certain reference value. Now I share it with you. Friends in need can refer to it

1. Keyword reply Text content

First we need to make some modifications in the text() method below LaneWeChat/core/aes/wechatrequest.lib.php. The code is as follows:

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';
        }
    }
Copy after login

Secondly we need to Start writing the backend 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(array('msg'=>'上传成功'));                        
      }else{
        $this->ajaxReturn(array('msg'=>'上传失败'));
      }
    }
  }
Copy after login

Enter keywords and reply content according to the front page to automatically reply to text content

2. Automatically reply to pictures based on keywords

According to the method in text() in LaneWeChat/core/aes/wechatrequest.lib.php, you can automatically determine the type of reply. Just get its type in the php background and automatically reply to the picture in PHP. Write the following code:

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'=>'上传失败')); 
      }
  }         
}
Copy after login

But please note that when replying to a picture, we should store a picture to be replied to in the data table in advance, so we also need to write the upload() method in PHP.

3. Reply to image and text messages based on keywords

is equivalent to replying to text and pictures in the same way. You only need to make slight modifications to obtain valid fields. .

Related recommendations:

PHP WeChat development of automatic text reply

PHP WeChat development of translation function

PHP WeChat development to obtain WeChat selected articles


The above is the detailed content of PHP WeChat development automatic reply. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template