微信开发二【自定义菜单】

<?php

/**
 * 微信--自定义菜单
 */

class Wechat_Menu
{

    /**
     * 获取acess_token
     */
    public function Access_Token($appid, $appsecret)
    {
//         $appid = "";
//         $appsecret = "";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        $jsoninfo = json_decode($output, true);
        $access_token = $jsoninfo["access_token"];
        return $access_token;
    }
    
    /**
     * 生成自定义菜单栏
     */
    public function Creat_Menu($access_token)
    {
//         $access_token = "";
        
        $jsonmenu = '{
              "button":[
              {
                    "name":"现代产品",
                   "sub_button":[
                    {
                       "type":"click",
                       "name":"明星专栏",
                       "key":"天气北京"
                    },
                    {
                        "type":"view",
                        "name":"新闻专区",
                        "url":"http://m.hao123.com/a/tianqi"
                    }]
              
        
               },
               {
                   "name":"现代服务",
                   "sub_button":[
                    {
                       "type":"click",
                       "name":"用户中心",
                       "key":"company"
                    },
                    {
                        "type":"click",
                        "name":"服务站查询",
                        "key":"笑话"
                    }]
               },
               {
                   "name":"我的现代",
                   "sub_button":[
                    {
                       "type":"click",
                       "name":"现代服务",
                       "key":"company"
                    },
                    {
                        "type":"click",
                        "name":"个人中心",
                        "key":"笑话"
                    }]
               
               }]
         }';
        
        
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
        $result = $this->https_request($url, $jsonmenu);
        var_dump($result);
        
    }
    
    /**
     * 自定义菜单--辅助
     */
    public 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;
    }
}

/**
*调用
*/
    /**
     *自定义菜单栏
     */
    public function menu()
    {
        $appid = "";
        $appsecret = "";
        
        $menuObj = new Wechat_Menu();
        //获取access_token
        $access_token = $menuObj->Access_Token($appid, $appsecret);
        //生成菜单栏
        $menu = $menuObj->Creat_Menu($access_token);
    }
?>



你可能感兴趣的:(微信开发)