1. 程式人生 > >微信公眾號開發(一) -- 自定義選單 動態選單

微信公眾號開發(一) -- 自定義選單 動態選單

簡單修改一下appid、appsecret 程式碼可直接複製使用

自定義選單

//獲取token值
$appid = ''; //微信支付申請對應的公眾號的APPID
$appsecret = ''; ////微信支付申請對應的公眾號的APP Key
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res = http_curl($url,'get','json');
$access_token = $res['access_token'];

function http_curl($url,$type='get',$res='json',$arr=''){
    //1.例項化curl
    $ch = curl_init();
    //2.設定curl引數
    curl_setopt($ch,CURLOPT_URL,$url);//要訪問的url地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//對認證證書的來源檢查
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//從證書中檢查SSL加密演算法是否存在
    if($type=='post'){
        curl_setopt($ch, CURLOPT_POST, 1);//傳送一個常規的POST請求
        curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);//post提交的資料包
    }
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//獲取的資訊以檔案流的形式返回

    //3.採集

    $output = curl_exec($ch);//執行操作
    if($res=='json'){
        if(curl_errno($ch)){
            return curl_error($ch);
        }else{
            return json_decode($output,true);
        }
    }
    //4.關閉
    curl_close($ch);
}

$jsonmenu='{
     "button":[
       {
           "name":"註冊",
           "sub_button":[
            {
               "type":"view",
               "name":"我是阿姨",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"我是客戶",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"我要開店",
               "url":"https://www.baidu.com/"
            }]
       },
      {
           "type":"view",
           "name":"預約服務",
           "url":"https://www.baidu.com/"
       },
       {
           "name":"服務介紹",
           "sub_button":[

            {
               "type":"view",
               "name":"產品介紹",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"企業介紹",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"線上客服",
               "url":"https://www.baidu.com/"
            }]
       }]
 }';

//包含Accesstoken.php主要是獲得access_token的功能,具體可以檢視我的另一篇部落格,微信公眾號開發獲取accesstoken的文章,很簡單的。

$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$result = https_request($url, $jsonmenu);
var_dump($result);//檢視返回資料

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);
    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;
}

動態選單
demo是根據標籤不同

$appid = ''; //微信支付申請對應的公眾號的APPID
$appsecret = ''; ////微信支付申請對應的公眾號的APP Key
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res = http_curl($url,'get','json');
$access_token = $res['access_token'];

function http_curl($url,$type='get',$res='json',$arr=''){
    //1.例項化curl
    $ch = curl_init();
    //2.設定curl引數
    curl_setopt($ch,CURLOPT_URL,$url);//要訪問的url地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//對認證證書的來源檢查
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//從證書中檢查SSL加密演算法是否存在
    if($type=='post'){
        curl_setopt($ch, CURLOPT_POST, 1);//傳送一個常規的POST請求
        curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);//post提交的資料包
    }
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//獲取的資訊以檔案流的形式返回

    //3.採集

    $output = curl_exec($ch);//執行操作
    if($res=='json'){
        if(curl_errno($ch)){
            return curl_error($ch);
        }else{
            return json_decode($output,true);
        }
    }
    //4.關閉
    curl_close($ch);
}

$jsonmenu='{
     "button":[
      {
           "name":"訂單中心",
           "sub_button":[
            {
               "type":"view",
               "name":"釋出訂單",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"線上接單",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"我的訂單",
               "url":"https://www.baidu.com/"
            }]
       },
      {
           "name":"資訊中心",
           "sub_button":[
            {
               "type":"view",
               "name":"背景調查",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"客戶預約",
               "url":"https://www.baidu.com/"
            },
            {
               "type":"view",
               "name":"服務人員",
               "url":"https://www.baidu.com/"
            }]
       },
       {
           "type":"view",
           "name":"商戶中心",
           "url":"https://www.baidu.com/"
       }],
       "matchrule":{
        "tag_id":"103"
      }
 }';


$url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=".$access_token;
$result = https_request($url, $jsonmenu);
var_dump($result);
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);
    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;
}

刪除選單

// 刪除個性化選單

//獲取token值
$appid = ''; //微信支付申請對應的公眾號的APPID
$appsecret = ''; ////微信支付申請對應的公眾號的APP Key
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res = http_curl($url,'get','json');
$access_token = $res['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
$result = https_request($url);

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);
    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;
}