一, 首先找到微擎关键字触发发送消息的 PHP 文件在 framework->builtin->core->processor.PHP 文件里面
二, 修改 respond 方法
- public function respond() {
- $result = $this->msg_respond();
- return $this->respText($result);
- }
三, 写入我们自己实现发送图文的方法
msg_respond() 方法
- private function msg_respond() {
- $rids = !is_array($this->rule) ? explode(',', $this->rule) : $this->rule;
- // 数据库中获取发送文字的信息
- $reply = table('basic_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
- // 图片
- $img_reply = table('images_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
- // 图文
- //$news_reply = table('news_reply')->where(array('rid IN' => $rids,'parent_id ==' => -1))->orderby('id')->getAll();
- $news_reply = table('news_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
- // 音乐
- $music_reply = table('music_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
- // 语音
- $voice_reply = table('voice_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
- // 视频
- $video_reply = table('video_reply')->where(array('rid IN' => $rids))->orderby('id')->getAll();
- // 父级找子级
- /*foreach ($news_reply as &$value){
- //if ($value['parent'] != -1) {
- $value['parent_data'] = table('news_reply')->where(['parent_id' => $value['id']])->orderby('id')->getAll();
- //}
- }*/
- // 判断是否为空, 如果都为空返回 false
- if (empty($reply)&&empty($img_reply)&&empty($news_reply)&&empty($music_reply)&&empty($voice_reply)&&empty($video_reply)) {
- return false;
- }
- $access_token=$this->getToken();
- $postStr=file_get_contents('php://input');
- $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- if (count($reply)+count($img_reply)+count($news_reply)+count($music_reply)+count($voice_reply)+count($video_reply)==1){
- if($reply!=null){
- $reply[0]['content'] = htmlspecialchars_decode($reply[0]['content']);
- $reply[0]['content'] = str_replace(array('<br>', ' '), array("\n", ''), $reply[0]['content']);
- $reply[0]['content'] = strip_tags($reply[0]['content'], '<a>');
- return $reply[0]['content'];
- }elseif ($img_reply!=null){
- for ($y=0;$y<count($img_reply);$y++){
- $this->imageReply($postObj->FromUserName,$access_token,$img_reply[$y]['mediaid']);
- }
- }elseif ($news_reply!=null){
- //$this->judgeType('news');
- for ($j=0;$j<count($news_reply);$j++){
- //$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']);
- $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']);
- }
- }elseif ($music_reply!=null){
- $result = $this->music_respond();
- return $this->respMusic(array(
- 'Title' => $result['title'],
- 'Description' => $result['description'],
- 'MusicUrl' => $result['url'],
- 'HQMusicUrl' => $result['hqurl'],
- ));
- }elseif ($voice_reply!=null){
- for ($s=0;$s<count($voice_reply);$s++){
- $this->voiceReply($postObj->FromUserName,$access_token,$voice_reply[$s]['mediaid']);
- }
- }elseif ($video_reply!=null){
- for ($d=0;$d<count($video_reply);$d++){
- $this->videoReply($postObj->FromUserName,$access_token,$video_reply[$d]['mediaid'],$video_reply[$d]['title'],$video_reply[$d]['description']);
- }
- }else{
- return "数据错误!!!";
- }
- }else{
- // 循环发送图片
- for ($y=0;$y<count($img_reply);$y++){
- $this->imageReply($postObj->FromUserName,$access_token,$img_reply[$y]['mediaid']);
- }
- // 循环发送图文
- for ($j=0;$j<count($news_reply);$j++){
- $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']);
- }
- // 视频
- for ($d=0;$d<count($video_reply);$d++){
- $this->videoReply($postObj->FromUserName,$access_token,$video_reply[$d]['mediaid'],$video_reply[$d]['title'],$video_reply[$d]['description']);
- }
- // 语音
- for ($s=0;$s<count($voice_reply);$s++){
- $this->voiceReply($postObj->FromUserName,$access_token,$voice_reply[$s]['mediaid']);
- }
- // 循环发送文字
- for($i=0;$i<count($reply);$i++){
- if($i==count($reply)-1){
- return $reply[$i]['content'];
- }else{
- $this->replymsg($postObj->FromUserName,$access_token,trim($reply[$i]['content']));
- }
- }
- }
- return 0;
- }
四, 获取 access token 信息
我们自己写一个 getToken() 方法, 需要修改 $appid 和 $secret 参数, 修改为你自己公众号上的 appid 和 secret
- private function getToken(){
- $appid = "你自己的 appid";
- $secret = "你自己的 secret";
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
- $ch = curl_init();// 初始化
- curl_setopt($ch, CURLOPT_URL, $url);// 与 url 建立对话
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 进行配置
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 进行配置
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 进行配置
- $output = curl_exec($ch);// 获取 Access Token
- curl_close($ch);// 关闭会话
- $jsoninfo = json_decode($output, true);
- $access_token =$jsoninfo["access_token"];
- echo $access_token;
- return $access_token;
- }
五, 写发送的方法
- // 发送文字的方法
- private function replymsg($fromUsername,$access_token,$content){
- $data = '{
- "touser":"'.$fromUsername.'",
- "msgtype":"text",
- "text":
- {
- "content":"'.$content.'"
- }
- }';
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
- //$this->https_post($url,$data);
- $result = $this->https_post($url,$data);
- $final = json_decode($result);
- return $final;
- }
- // 发送图片的方法
- private function imageReply($fromUsername,$access_token,$mediaId){
- $data='{
- "touser":"'.$fromUsername.'",
- "msgtype":"image",
- "image":
- {
- "media_id":"'.$mediaId.'"
- }
- }';
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
- //$this->https_post($url,$data);
- $result = $this->https_post($url,$data);
- $final = json_decode($result);
- return $final;
- }
- // 发送图文的方法
- private function newsReply($fromUsername,$access_token,$url,$picUrl,$title,$description,$mediaid){
- $data = '{
- "touser":"'.$fromUsername.'",
- "msgtype":"mpnews",
- "mpnews":{
- "media_id":"'.$mediaid.'"
- }
- }';
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
- //$this->https_post($url,$data);
- $result = $this->https_post($url,$data);
- $final = json_decode($result);
- return $final;
- }
- // 发送音乐的方法
- private function musicReply($fromUsername,$access_token,$title,$description,$url,$hqurl){
- $data = '{
- "touser":"'.$fromUsername.'",
- "msgtype":"music",
- "music":
- {
- "title":"'.$title.'",
- "description":"'.$description.'",
- "musicurl":"'.$url.'",
- "hqmusicurl":"'.$hqurl.'"
- }
- }';
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
- //$this->https_post($url,$data);
- $result = $this->https_post($url,$data);
- $final = json_decode($result);
- return $final;
- }
- // 语音
- private function voiceReply($fromUsername,$access_token,$mediaid){
- $data='
- {
- "touser":"'.$fromUsername.'",
- "msgtype":"voice",
- "voice":
- {
- "media_id":"'.$mediaid.'"
- }
- }
- ';
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
- //$this->https_post($url,$data);
- $result = $this->https_post($url,$data);
- $final = json_decode($result);
- return $final;
- }
- // 视频
- private function videoReply($fromUsername,$access_token,$mediaid,$title,$description){
- $data = '
- {
- "touser":"'.$fromUsername.'",
- "msgtype":"video",
- "video":
- {
- "media_id":"'.$mediaid.'",
- "title":"'.$title.'",
- "description":"'.$description.'"
- }
- }
- ';
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
- //$this->https_post($url,$data);
- $result = $this->https_post($url,$data);
- $final = json_decode($result);
- return $final;
- }
六, https_post 方法
- private function https_post($url,$data)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($curl);
- if (curl_errno($curl)) {
- return 'Errno'.curl_error($curl);
- }
- curl_close($curl);
- return $result;
- }
7, 展示一下效果吧
八, 觉得本文章对你有帮助的麻烦动下手指点个赞呗
来源: http://www.bubuko.com/infodetail-3350578.html