微信公衆號接口生成菜單(連接、文本回復及圖文消息)

一、獲取access_token
二、獲取圖文消息media_id
三、肯定文本回復key,構建菜單json字符串
四、請求菜單建立接口json

一、獲取access_token
方便起見,用微信公衆平臺接口調試工具(http://mp.weixin.qq.com/debug?token=1606511159&lang=zh_CN)獲取access_tokenapi

 

獲取過程可能會遇到白名單問題,在公衆號開發基本配置中臨時修改下白名單,不出意外access_token獲取成功安全

 

二、因菜單包含圖文消息菜單,需獲取圖文消息media_id,方便起見利用postman調用接口獲取。(上述第一步獲取access_token也能夠直接用postman)微信

調試公衆號POST請求(得到後臺素材media_id)微信公衆平臺

接口地址ide

http請求方式: POST
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN工具

參數說明post

參數 是否必須 說明
type 素材的類型,圖片(image)、視頻(video)、語音 (voice)、圖文(news)
offset 從所有素材的該偏移位置開始返回,0表示從第一個素材 返回
count 返回素材的數量,取值在1到20之間

將剛剛得到的基礎token拼入接口地址填入postman 選擇請求方式爲POSTthis

 

添加請求參數url

 

 返回結果中獲取media_id

 

三、包含直接回覆文本的菜單,肯定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"}

相關文章
相關標籤/搜索