微信公众号开发者模式下的自定义菜单回复图片和图文
话不多说,我分析我遇到的问题和解决方法。
1 注意的是,区分好个人订阅号和测试号;
1.1:个人订阅号,在没有通过验证前,是不可以在开发者模式下自定义菜单。所以一般都选择测试号学习开发。以下也以测试号来说明情况。
2.先获取token值;
2.1 注意标红处
先点图一
然后返回刚刚的图二;
记住怎么获取token值,等等需要用到。
自定义菜单代码(php)
<?php /** * Created by PhpStorm. * User: * Date: 2020/6/5 * Time: 10:05 */ $appid = "自己的测试号的appid "; $appsecret = "自己的测试号的appsecret "; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $output = https_request($url); $result = (array)json_decode($output); $token = $result[\'access_token\']; //调用自定义菜单接口创建自定义菜单 $urlMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$token"; $MenuData = \' { "button":[ { "name":"实验资料", "sub_button":[ { "type":"view", "name":"实验三", "url":"https://www.baidu.com/" }, { "type":"click", "name":"实验二", "key":"V1001_GOOD_2" }, { "type":"click", "name":"实验一", "key":"V1001_GOOD_1" } ] }, { "type":"click", "name":"课程导学", "key":"V1001_GOOD_3" }, { "name":"课外提升", "sub_button":[ { "type":"click", "name":"微信年度数据报告", "key":"V1001_GOOD_4" },{ "type":"click", "name":"微信开发学习路程", "key":"V1001_GOOD_5" }] }] } \'; $output1 = https_request($urlMenu,$MenuData); echo $output1; function https_request($url,$data = null) { $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_RETURNTRANSFER,1); if(!empty($data)) { curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$data); } curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); $output = curl_exec($curl); curl_close($curl); return $output; } ?>代码二:
<?php define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET[\'echostr\'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, \'SimpleXMLElement\', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); //用户发送的消息类型判断 switch ($RX_TYPE) { case "event": //文本消息 $result = $this->receiveEvent($postObj); break; case "text": //文本消息 $result = $this->receiveText($postObj); break; case "image": //图片消息 $result = $this->receiveImage($postObj); break; case "voice": //语音消息 $result = $this->receiveVoice($postObj); break; case "video": //视频消息 $result = $this->receiveVideo($postObj); break; case "location"://位置消息 $result = $this->receiveLocation($postObj); break; case "link": //链接消息 $result = $this->receiveLink($postObj); break; default: $result = "unknow msg type: ".$RX_TYPE; break; } echo $result; //返回处理的结果 }else { echo " "; exit; } } //接收文本消息 private function receiveEvent($object) { $Event = $object->Event; if($Event =="subscribe"){ $content = "欢迎关注小抓瓦"."\n"."我们因你而精彩"; }elseif($Event == "CLICK"){ $Key = $object->EventKey; if($Key == "V1001_GOOD_2"){ $result = $this->transmitnews($object); return $result; } elseif($Key == "V1001_GOOD_1"){ $result = $this->transmitImage1($object); return $result; } elseif($Key == "V1001_GOOD_3"){ $result = $this->transmitMusic($object); return $result; } elseif($Key == "V1001_GOOD_4"){ $result = $this->transmitnews1($object); return $result; } elseif($Key == "V1001_GOOD_5"){ $result = $this->transmitnews2($object); return $result; } else { $content = "你点击了CLICK菜单"; } } else{ $content = "你点击了菜单"; } $result = $this->transmitText($object, $content); return $result; } private function receiveText($object) { $keyworlds = $object->Content; if($keyworlds == "天后"){ $result = $this->transmitMusic($object); } elseif($keyworlds == "你好"){ $content = "你好呀。"; $result = $this->transmitText($object, $content); }elseif($keyworlds == "图片"){ $result = $this->transmitImage1($object); }elseif($keyworlds == "图文"){ $result = $this->transmitnews($object); } else{ $content = "你发送的是文本,内容为:".$object->Content; $result = $this->transmitText($object, $content); } return $result; } //接收图片消息 private function receiveImage($object) { $content =array("MediaId"=>$object->MediaId); $result = $this->transmitImage($object, $content); return $result; } //接收语音消息 private function receiveVoice($object) { $content =array("MediaId"=>$object->MediaId); $result = $this->transmitVoice($object, $content); return $result; } //接收视频消息 private function receiveVideo($object) { $content = "你发送的是视频,媒体ID为:".$object->MediaId; $result = $this->transmitText($object, $content); return $result; } //接收位置消息 private function receiveLocation($object) { $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label; $result = $this->transmitText($object, $content); return $result; } //接收链接消息 private function receiveLink($object) { $content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url; $result = $this->transmitText($object, $content); return $result; } //回复文本消息 private function transmitText($object, $content) { $textTpl = " <![CDATA[%s]]> <![CDATA[%s]]> %s <![CDATA[text]]> <![CDATA[%s]]> "; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } //回复图片消息 private function transmitImage1($object) { $itemTpl=" <![CDATA[%s]]> <![CDATA[%s]]> %s <![CDATA[image]]> <![CDATA[q72tuNgtah6dELq4sCP1juldRLiCqD5Tgztj2QuDpjR-vwJj3S7QuMOV20x4A8Uy]]> "; $result = sprintf($itemTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } ?>讲下重点代码:
图文等等是推送是类似的。今天就到这,希望能帮助到你们,我也是小白。