微信自定義菜單的處理開發(fā)示例

字號:


    自定義菜單的創(chuàng)建
    <?php
    define("APPID", "您的appid");
    define("APPSECRET", "您的appsecret ");
    $token_access_url = "=" . APPID . "&secret=" . APPSECRET;
    $res = file_get_contents($token_access_url); //獲取文件內(nèi)容或獲取網(wǎng)絡請求的內(nèi)容
    //echo $res;
    $result = json_decode($res, true); //接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量
    $access_token = $result['access_token'];
    define("ACCESS_TOKEN", $access_token); //將access_token定義為常量,便于使用.
    $make_menu_url = "=" . ACCESS_TOKEN;
    $menuData = ' {
    "button":[
    {
    "type":"click",
    "name":"今日歌曲",
    "key":"V1001_TODAY_MUSIC"
    },
    {
    "name":"菜單",
    "sub_button":[
    {
    "type":"view",
    "name":"搜索",
    "url":""
    },
    {
    "type":"view",
    "name":"視頻",
    "url":""
    },
    {
    "type":"click",
    "name":"贊一下我們",
    "key":"V1001_GOOD"
    }]
    }]
    }';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $make_menu_url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $menuData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $info = curl_exec($ch);
    //判讀執(zhí)行過程中是否有錯誤,有則發(fā)送數(shù)據(jù)錯誤報告.
    if (curl_errno($ch)) {
    echo 'Error' . curl_error($ch); //用戶檢查php運行環(huán)境中的curl模塊開啟情況.
    }
    curl_close($ch);
    print_r($info); //查看post提交到微信服務器后,返回的數(shù)據(jù).
    自定義菜單的獲取
    <?php
    define("APPID", "您的appid");
    define("APPSECRET", "您的appsecret ");
    $token_access_url = "=" . APPID . "&secret=" . APPSECRET;
    $res = file_get_contents($token_access_url); //獲取文件內(nèi)容或獲取網(wǎng)絡請求的內(nèi)容
    $result = json_decode($res, true); //接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量
    $access_token = $result['access_token'];
    $make_menu_url = "=" . $access_token;
    $menu_json = file_get_contents($make_menu_url);
    echo $menu_json;
    自定義菜單的刪除
    <?php
    define("APPID", "您的appid");
    define("APPSECRET", "您的appsecret ");
    $token_access_url = "=" . APPID . "&secret=" . APPSECRET;
    $res = file_get_contents($token_access_url); //獲取文件內(nèi)容或獲取網(wǎng)絡請求的內(nèi)容
    $result = json_decode($res, true); //接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量
    $access_token = $result['access_token'];
    $make_menu_url = "=" . $access_token;
    $menu_json = file_get_contents($make_menu_url);
    echo $menu_json;