码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2015-08-13 14:36:30      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

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



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

标签:

原文地址:http://my.oschina.net/hzfs/blog/491784

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!