昨天研究最近爆火的那个小霸王游戏的营销思路,说人话就是抄一下,其实思路就是网页引流到公众号,通过外卖佣金变现。
这次的坑,是我要实现下图的功能:
看起来很简单是不是,其实这个功能公众号自带的自动回复可以实现。
选择回复全部就可以回复多条消息,但微信公众号自身无法回复H5链接消息,也就是图中第一条消息样式。
于是考虑到了用微擎实现,接口对接,设置全都ok了,然后发现一个恶心的地方,【微擎竟然不能多条回复!!!!!】
我靠,这就尴尬了,微擎到现在这点简单的功能都不开放吗?别说他们不会开发,网上教程都出来了!
哎,这样我陷入了两难,双面都有bug,无法满足我的要求,装微擎模块能解决,可惜,免费模块都测试了不能用,收费的~呵呵,这点功能不值得花钱啊!
我也懒得换程序了,于是开始魔改一下,让微擎自己能实现多条回复功能。

消息回复功能写在这个文件里,打开文件,是一个名为CoreModuleProcessor的类,首先把类中第一个函数修改如下:
1 2 3 4 |
public function respond() { $result = $this->msg_respond(); return $this->respText($result); } |
然后插入我们的循环发送函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
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))->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(); //判断是否为空,如果都为空返回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>', '&nbsp;'), 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->newsRespond($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']); // $this->newsRespond(); } }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->newsRespond($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的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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; } |
插入请求函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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; } |
重写所有发送方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
//发送文字的方法 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 newsRespond($fromUsername,$access_token,$url,$picUrl,$title,$description,$mediaid) { $myfile = fopen("newfile.txt", "w") ; fwrite($myfile, $mediaid); fclose($myfile); if(preg_match("/^\d*$/",$mediaid)) { $data = '{ "touser":"'.$fromUsername.'", "msgtype":"news", "news":{ "articles": [ { "title":"'.$title.'", "description":"'.$description.'", "url":"'.$url.'", "picurl":"'.$picUrl.'" } ] } }'; $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; } else{ $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; } |
OK,这一套下来基本就是重写了整个微擎的回复代码,原则上用上面的这些方法替换掉CoreModuleProcessor类中的所有代码,就可以了。
虽然我这些代码也是按照网上复制粘贴的,但网上的原版魔改教程是有个bug的,那就是他没有写图文链接消息的回复代码,也就是最后实现了多条回复,但又无法回复图文链接消息,这点我已经修复了,也就是我这个代码目前是比较完善的魔改方法。
最后终于实现了想要的功能:
这个小霸王游戏机的流程,也终于被我搞定,其实在小霸王网页的代码中还有一个坑,那就是你缺少后台php文件接口,所以你搭建完的无法定义分享标题和封面图,这点我也已经搞定了,自己写了个后台接口,涉及知识就是微信公众号的网页开发,有能力的朋友去看看文档吧!累了,最后看看成果!