微信公衆號開發--自定義菜單

代碼在最下面,轉載請在明顯位置加入本篇博客地址

需要注意的:

1、自定義菜單最多包括3個一級菜單,每個一級菜單最多包含5個二級菜單。
2、一級菜單最多4個漢字,二級菜單最多7個漢字,多出來的部分將會以“...”代替。
3、創建自定義菜單後,菜單的刷新策略是,在用戶進入公衆號會話頁或公衆號profile頁時,如果發現上一次拉取菜單的請求在5分鐘以前,就會拉取一下菜單,如果菜單有更新,就會刷新客戶端的菜單。測試時可以嘗試取消關注公衆賬號後再次關注,則可以看到創建後的效果。

自定義菜單接口可實現多種類型按鈕,如下:

1、click:點擊推事件
2、view:跳轉URL
3、scancode_push:掃碼推事件
4、scancode_waitmsg:掃碼推事件且彈出“消息接收中”提示框
5、pic_sysphoto:彈出系統拍照發圖
6、pic_photo_or_album:彈出拍照或者相冊發圖
7、pic_weixin:彈出微信相冊發圖器
8、location_select:彈出地理位置選擇器
9、media_id:下發消息(除文本消息)
10、view_limited:跳轉圖文消息URL

一般常用1與2,其餘或詳細解說請到官方網站查看(http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html)

先獲取acces_token

接口地址:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

請求方式:GET

參數說明:

參數 是否必須 說明
grant_type 獲取access_token填寫client_credential
appid 第三方用戶唯一憑證
secret 第三方用戶唯一憑證密鑰,即appsecret

正常返回的數據格式:

{"access_token":"ACCESS_TOKEN","expires_in":7200}

access_token 獲取到的憑證,自定義菜單會用到

expires_in 過期時間,用於緩存access_token,避免每次都獲取。

錯誤時微信會返回錯誤碼等信息:

{"errcode":40013,"errmsg":"invalid appid"}

errcode  錯誤碼

errmsg 錯誤信息

創建自定義菜單

接口地址: https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN   (此處的access_token上面已經獲取到直接用)

請求方式: POST

提交數據(click與view示例,其餘類型請查看官方文檔):

{
     "button":[
     {  
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "name":"菜單",
           "sub_button":[
           {    
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"視頻",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"贊一下我們",
               "key":"V1001_GOOD"
            }]
       }]
 }

參數說明:

參數 是否必須 說明
button 一級菜單數組,個數應爲1~3個
sub_button 二級菜單數組,個數應爲1~5個
type 菜單的響應動作類型
name 菜單標題,不超過16個字節,子菜單不超過40個字節
key click等點擊類型必須 菜單KEY值,用於消息接口推送,不超過128字節
url view類型必須 網頁鏈接,用戶點擊菜單可打開鏈接,不超過1024字節
media_id media_id類型和view_limited類型必須 調用新增永久素材接口返回的合法media_id

返回結果:

正確時的返回JSON數據包如下:

{"errcode":0,"errmsg":"ok"}

錯誤時的返回JSON數據包如下(示例爲無效菜單名長度):

{"errcode":40018,"errmsg":"invalid button name size"}

源碼:

$appid        = '123456';
$appsecret    = '123456';
$apiUrlPrefix = 'https://api.weixin.qq.com/cgi-bin';

//注意,獲取access_token有調用次數限制,請自行做緩存
$result = httpget($apiUrlPrefix.'/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret);
if($result) {
    $json = json_decode($result,true);
    if (!$json || isset($json['errcode'])) {
        //獲取失敗,業務處理
        exit;
    }
    $expire = intval($json['expires_in'])-100; //過期時間
    $access_token = $json['access_token'];
} else {
    //獲取失敗,業務處理
    exit;
}


$data = ' {
     "button":[
     {  
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "name":"菜單",
           "sub_button":[
           {    
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"視頻",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"贊一下我們",
               "key":"V1001_GOOD"
            }]
       }]
 }';

$result = httppost($apiUrlPrefix.'/menu/create?access_token='.$access_token,$data);
if ($result) {
    $json = json_decode($result,true);
    if (!$json || !empty($json['errcode'])) {
        //失敗,業務處理
        exit;
    }
    //成功
} else {
    //失敗,業務處理
    exit;
}


function httpget($url){
    $oCurl = curl_init();
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if(intval($aStatus["http_code"])==200){
        return $sContent;
    }else{
        return false;
    }
}

function httppost($url,$param,$post_file=false){
    $oCurl = curl_init();
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    if (is_string($param) || $post_file) {
        $strPOST = $param;
    } else {
        $aPOST = array();
        foreach($param as $key=>$val){
            $aPOST[] = $key."=".urlencode($val);
        }
        $strPOST =  join("&", $aPOST);
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($oCurl, CURLOPT_POST,true);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if(intval($aStatus["http_code"])==200){
        return $sContent;
    }else{
        return false;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章