微信公众号接口生成菜单(链接、文本回复及图文消息)
1、获取access_token
2、获取图文消息media_id
3、确定文本回复key,构建菜单json字符串
4、请求菜单创建接口
1、获取access_token
方便起见,用微信公众平台接口调试工具(http://mp.weixin.qq.com/debug?token=1606511159&lang=zh_CN)获取access_token
获取过程可能会遇到白名单问题,在公众号开发基本配置中临时修改下白名单,不出意外access_token获取成功
2、因菜单包含图文消息菜单,需获取图文消息media_id,方便起见利用postman调用接口获取。(上述第一步获取access_token也可以直接用postman)
调试公众号POST请求(获得后台素材media_id)
接口地址
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN
参数说明
参数 | 是否必须 | 说明 |
---|---|---|
type | 是 | 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news) |
offset | 是 | 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回 |
count | 是 | 返回素材的数量,取值在1到20之间 |
将刚刚获得的基础token拼入接口地址填入postman 选择请求方式为POST
添加请求参数
返回结果中获取media_id
3、包含直接回复文本的菜单,确定key,构建菜单json字符串
$jsonMenu = ' { "button": [ { "name": "资源管理", "sub_button": [ { "type": "view", "name": "我的记录", "url": "http://m.test.net/h5/my/index", "sub_button": [ ] }, { "type": "click", "name": "使用说明1", "key": "SYSM-001", "sub_button": [ ] }, { "type": "click", "name": "使用说明2", "key": "SYSM-002", "sub_button": [ ] } ] }, { "name": "加盟培训", "sub_button": [ { "type": "view", "name": "安全通告", "url": "http://mp.weixin.qq.com/s/haHruj5RgoawpYOoEUA1CA", "sub_button": [ ] }, { "type": "view_limited", "name": "图文消息1", "media_id": "O4NvReZyZPUCT4efE3q6FwtfaTmWyEgIbLg2arIGiA0" }, { "type": "view_limited", "name": "图文消息2", "media_id": "O4NvReZyZPUCTyefE3q6FwlTyoldJsDiHbFKCwdmPSc" }, ] }, ] }';
文本回复的菜单需在代码中作相应处理,检测event中click,匹配key值,回复对应文本消息内容
public function weixin(){ $postStr = $this->input->raw_input_stream; $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); if(!empty($postObj)){ $MsgType = $postObj->MsgType; switch($MsgType){ case "text": $resultStr = $this->_handle_text($postObj); break; case "event": $resultStr = $this->_handle_event($postObj); break; default: $resultStr = "Unknow msg type: ".$MsgType; break; } echo $resultStr; } else{ echo 'error'; } } function _handle_event($postObj){ $openid = strval($postObj->FromUserName); $EventKey = strval($postObj->EventKey); ll('Weixinlib'); $userinfo = $this->weixinlib->get_user_info($openid); // 获取平台用户信息 array $msgType = 'text'; switch (strtolower($postObj->Event)) { case "subscribe": $content = $this->_handle_event_subscribe($postObj,$userinfo); break; case "scan": $content = $this->_handle_event_scan($postObj,$userinfo); break; case "click"://文本消息回复处理 $content = $this->_handle_event_click($postObj); break; default : $content = "Unknow Event: ".$postObj->Event; break; } $resultStr = $this->_response($postObj, $content,$msgType); return $resultStr; } function _handle_event_click($postObj){ $scene = $postObj->EventKey; return $this->_click_handle_scene($scene); } function _click_handle_scene($scene){ switch ($scene){ case 'SYSM-001': $resultStr = "SYSM-001对应的文本消息"; break; case 'SYSM-002': $resultStr = "SYSM-001对应的文本消息"; break; default: $resultStr = "unknow click"; break; } return $resultStr; } function _response($object, $content,$msgType = 'text'){ if(empty($content)){ return; } if($msgType == 'text'){ return $this->_response_text($object, $content); }else if($msgType == 'news'){ //图文消息 return $this->_response_news($object, $content); } } private function _response_text($object, $content, $flag=0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; }
调用创建菜单接口,创建菜单
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token"; $output = http_post($url, $jsonMenu);
成功返回:{"errcode":0,"errmsg":"ok"}