微擎系統微信公衆號關鍵字觸發回覆多條消息實現

使用客服消息實現發送多條圖文消息

第一步、首先找到微擎關鍵字觸發發送消息的PHP文件在framework->builtin->core->processor.php文件裏面

2、修改respond方法

1 public function respond() { 2         $result = $this->msg_respond(); 3         return $this->respText($result); 4 }

3、寫入咱們本身實現發送圖文的方法

msg_respond()方法php

 1 private function msg_respond() {  2         $rids = !is_array($this->rule) ? explode(',', $this->rule) : $this->rule;  3         //數據庫中獲取發送文字的信息
 4         $reply = table('basic_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();  5         //圖片
 6         $img_reply = table('images_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();  7         //圖文  8  //$news_reply = table('news_reply')->where(array('rid IN' => $rids,'parent_id ==' => -1))->orderby('id')->getAll();
 9         $news_reply = table('news_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll(); 10         //音樂
11         $music_reply = table('music_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll(); 12         //語音
13         $voice_reply = table('voice_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll(); 14         //視頻
15         $video_reply = table('video_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll(); 16 //父級找子級
17         /*foreach ($news_reply as &$value){ 18  //if ($value['parent'] != -1) { 19  $value['parent_data'] = table('news_reply')->where(['parent_id' => $value['id']])->orderby('id')->getAll(); 20  //} 21  }*/
22 
23         //判斷是否爲空,若是都爲空返回false
24         if (empty($reply)&&empty($img_reply)&&empty($news_reply)&&empty($music_reply)&&empty($voice_reply)&&empty($video_reply)) { 25             return false; 26  } 27         $access_token=$this->getToken(); 28         $postStr=file_get_contents('php://input'); 29         $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 30         if (count($reply)+count($img_reply)+count($news_reply)+count($music_reply)+count($voice_reply)+count($video_reply)==1){ 31             if($reply!=null){ 32                 $reply[0]['content'] = htmlspecialchars_decode($reply[0]['content']); 33                 $reply[0]['content'] = str_replace(array('<br>', '&nbsp;'), array("\n", ' '), $reply[0]['content']); 34                 $reply[0]['content'] = strip_tags($reply[0]['content'], '<a>'); 35                 return $reply[0]['content']; 36             }elseif ($img_reply!=null){ 37                 for ($y=0;$y<count($img_reply);$y++){ 38                     $this->imageReply($postObj->FromUserName,$access_token,$img_reply[$y]['mediaid']); 39  } 40             }elseif ($news_reply!=null){ 41                 //$this->judgeType('news');
42                 for ($j=0;$j<count($news_reply);$j++){ 43 
44                     //$this->newsReply($postObj->FromUserName,$access_token,$news_reply[$j]['url'],$news_reply[$j]['thumb'],$news_reply[$j]['title'],$news_reply[$j]['description'],$news_reply[$j]['parent_data']);
45                     $this->newsReply($postObj->FromUserName,$access_token,$news_reply[$j]['url'],$news_reply[$j]['thumb'],$news_reply[$j]['title'],$news_reply[$j]['description'],$news_reply[$j]['media_id']); 46  } 47             }elseif ($music_reply!=null){ 48                 $result = $this->music_respond(); 49                 return $this->respMusic(array( 50                     'Title' => $result['title'],
51                     'Description' => $result['description'],
52                     'MusicUrl' => $result['url'],
53                     'HQMusicUrl' => $result['hqurl'],
54  )); 55             }elseif ($voice_reply!=null){ 56                 for ($s=0;$s<count($voice_reply);$s++){ 57                     $this->voiceReply($postObj->FromUserName,$access_token,$voice_reply[$s]['mediaid']); 58  } 59             }elseif ($video_reply!=null){ 60                 for ($d=0;$d<count($video_reply);$d++){ 61                     $this->videoReply($postObj->FromUserName,$access_token,$video_reply[$d]['mediaid'],$video_reply[$d]['title'],$video_reply[$d]['description']); 62  } 63             }else{ 64                 return "數據錯誤!!!"; 65  } 66         }else{ 67             //循環發送圖片
68             for ($y=0;$y<count($img_reply);$y++){ 69                 $this->imageReply($postObj->FromUserName,$access_token,$img_reply[$y]['mediaid']); 70  } 71             //循環發送圖文
72             for ($j=0;$j<count($news_reply);$j++){ 73                 $this->newsReply($postObj->FromUserName,$access_token,$news_reply[$j]['url'],$news_reply[$j]['thumb'],$news_reply[$j]['title'],$news_reply[$j]['description'],$news_reply[$j]['media_id']); 74  } 75             //視頻
76             for ($d=0;$d<count($video_reply);$d++){ 77                 $this->videoReply($postObj->FromUserName,$access_token,$video_reply[$d]['mediaid'],$video_reply[$d]['title'],$video_reply[$d]['description']); 78  } 79             //語音
80             for ($s=0;$s<count($voice_reply);$s++){ 81                 $this->voiceReply($postObj->FromUserName,$access_token,$voice_reply[$s]['mediaid']); 82  } 83             //循環發送文字
84             for($i=0;$i<count($reply);$i++){ 85                 if($i==count($reply)-1){ 86                     return $reply[$i]['content']; 87                 }else{ 88                     $this->replymsg($postObj->FromUserName,$access_token,trim($reply[$i]['content'])); 89  } 90  } 91  } 92         return 0; 93     }

4、獲取access token信息

咱們本身寫一個getToken()方法,須要修改$appid和$secret參數,修改成你本身公衆號上的appid和secrethtml

 1 private    function getToken(){  2         $appid = "你本身的appid";  3         $secret = "你本身的secret";  4         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;  5         $ch = curl_init();//初始化
 6         curl_setopt($ch, CURLOPT_URL, $url);//與url創建對話
 7         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //進行配置
 8         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //進行配置
 9         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//進行配置
10         $output = curl_exec($ch);//獲取Access Token
11         curl_close($ch);//關閉會話
12         $jsoninfo = json_decode($output, true); 13         $access_token =$jsoninfo["access_token"]; 14         echo $access_token; 15         return $access_token; 16     }

5、寫發送的方法

 1 //發送文字的方法
 2     private function replymsg($fromUsername,$access_token,$content){  3 
 4         $data = '{  5             "touser":"'.$fromUsername.'",  6  "msgtype":"text",  7  "text":  8  {  9                  "content":"'.$content.'"  10  }  11         }';  12 
 13         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;  14         //$this->https_post($url,$data);
 15         $result = $this->https_post($url,$data);  16         $final = json_decode($result);  17         return $final;  18  }  19     //發送圖片的方法
 20     private function imageReply($fromUsername,$access_token,$mediaId){  21         $data='{  22             "touser":"'.$fromUsername.'",  23  "msgtype":"image",  24  "image":  25  {  26                     "media_id":"'.$mediaId.'"  27  }  28         }';  29         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;  30         //$this->https_post($url,$data);
 31         $result = $this->https_post($url,$data);  32         $final = json_decode($result);  33         return $final;  34  }  35     //發送圖文的方法
 36     private function newsReply($fromUsername,$access_token,$url,$picUrl,$title,$description,$mediaid){  37         $data = '{  38             "touser":"'.$fromUsername.'",  39  "msgtype":"mpnews",  40  "mpnews":{  41                 "media_id":"'.$mediaid.'"  42  }  43         }';  44         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;  45         //$this->https_post($url,$data);
 46         $result = $this->https_post($url,$data);  47         $final = json_decode($result);  48         return $final;  49  }  50     //發送音樂的方法
 51     private function musicReply($fromUsername,$access_token,$title,$description,$url,$hqurl){  52         $data = '{  53             "touser":"'.$fromUsername.'",  54  "msgtype":"music",  55  "music":  56  {  57                 "title":"'.$title.'",  58                 "description":"'.$description.'",  59                 "musicurl":"'.$url.'",  60                 "hqmusicurl":"'.$hqurl.'"  61  }  62         }';  63         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;  64         //$this->https_post($url,$data);
 65         $result = $this->https_post($url,$data);  66         $final = json_decode($result);  67         return $final;  68  }  69     //語音
 70     private function voiceReply($fromUsername,$access_token,$mediaid){  71         $data='
 72  {  73     "touser":"'.$fromUsername.'",  74  "msgtype":"voice",  75  "voice":  76  {  77       "media_id":"'.$mediaid.'"  78  }  79 }  80         ';  81         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;  82         //$this->https_post($url,$data);
 83         $result = $this->https_post($url,$data);  84         $final = json_decode($result);  85         return $final;  86  }  87     //視頻
 88     private function videoReply($fromUsername,$access_token,$mediaid,$title,$description){  89         $data = '
 90  {  91     "touser":"'.$fromUsername.'",  92  "msgtype":"video",  93  "video":  94  {  95       "media_id":"'.$mediaid.'",  96       "title":"'.$title.'",  97       "description":"'.$description.'"  98  }  99 } 100         '; 101         $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token; 102         //$this->https_post($url,$data);
103         $result = $this->https_post($url,$data); 104         $final = json_decode($result); 105         return $final; 106     }

6、https_post方法

 1 private function https_post($url,$data)  2  {  3         $curl = curl_init();  4         curl_setopt($curl, CURLOPT_URL, $url);  5         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  6         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  7         curl_setopt($curl, CURLOPT_POST, 1);  8         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  9         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 10         $result = curl_exec($curl); 11         if (curl_errno($curl)) { 12             return 'Errno'.curl_error($curl); 13  } 14         curl_close($curl); 15         return $result; 16     }

七、展現一下效果吧

8、以爲本文章對你有幫助的麻煩動下手指點個讚唄

原文出處:https://www.cnblogs.com/MingGyGy-Castle/p/12100343.html數據庫

相關文章
相關標籤/搜索