2013-0820

Php开发微信公众号自定义菜单功能

作者: momy 分类: 编程开发 4 Comment »
摘要:Php开发微信公众号自定义菜单功能,由于微信屏蔽了View类型的菜单,不能点击菜单跳转页面,只能使用click的菜单。

相关信息请参看微信公众号开发接口文档

下边是PHP版的菜单添加、删除、查询接口实现代码,包括Token获取代码:*1

private function menus_operate ()
{
    $type = empty($_GET['type']) ? '' : trim($_GET['type']);
    if (!$type)
    {
        echo 'operate fail,no operate type';
        return;
    }
    $password = empty($_GET['password']) ? '' : trim($_GET['password']);
    if (!$password||$password!='heshunneng')
    {
        echo 'operate fail,password error';
        return;
    }
    switch ($type)
    {
        case "create":
            echo $this->CreateMenus();
            break;
        case "query":
            echo $this->QueryMenus();
            break;
        case "delete":
            echo $this->DeleteMenus();
            break;
        case "get_token":
            echo $this->GetAccessToken();
            break;
        default:
            echo 'please enter the right  type!';
    }
}
private function GetAccessToken ()
{
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5d82defab*f&secret=f65cc3ceca3c537f974b875596*';
    $result = json_decode(file_get_contents($url));
    if (empty($result->{'access_token'}))
    {
        echo 'get AccessToken fail ' . $result->{'errcode'} . ' ' . $result->{'errmsg'}.'. ';
    }
    else
    {
        echo $result->{'access_token'}.'. '.$result->{'expires_in'};
    }
}
private function CreateMenus ()
{
    $access_token = empty($_GET['token']) ? '' : trim($_GET['token']);
    if (!$access_token)
    {
        echo 'operate fail,no access_token';
        return;
    }
    $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
    $data = '{
    "button":[
    {
    "name":"物流信息",
    "sub_button":[
    {
    "type":"click",
    "name":"找货",
    "key":"menu_huoyuan"
    },
    {
    "type":"click",
    "name":"找车",
    "key":"menu_cheyuan"
    },
    {
    "type":"click",
    "name":"找专线",
    "key":"menu_zhuanxian"
    },
    {
    "type":"click",
    "name":"物流园区",
    "key":"menu_yuanqu"
    },
    {
    "type":"click",
    "name":"仓库招租",
    "key":"menu_zhaozu"
    }]
    },
    {
    "name":"物流新闻",
    "sub_button":[
    {
    "type":"click",
    "name":"政策公告",
    "key":"menu_gonggao"
    },
    {
    "type":"click",
    "name":"物流新闻",
    "key":"menu_news"
    },
    {
    "type":"click",
    "name":"东盟动态",
    "key":"menu_dongmeng"
    },
    {
    "type":"click",
    "name":"天气查询",
    "key":"menu_weather"
    },
    {
    "type":"click",
    "name":"在途服务",
    "key":"menu_zaitu"
    }]
    },
    {
    "name":"业务信息",
    "sub_button":[
    {
    "type":"click",
    "name":"仓储业务",
    "key":"menu_contact"
    },
    {
    "type":"click",
    "name":"中转业务",
    "key":"menu_idea"
    },
    {
    "type":"click",
    "name":"在途业务",
    "key":"menu_about"
    },
    {
    "type":"click",
    "name":"客户查询",
    "key":"menu_about"
    }]
    }]
    }';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $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 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch))
    {
        echo 'Errno' . curl_error($ch);
    }
    curl_close($ch);
    $result=json_decode($tmpInfo);
    echo ' '.$result->errcode.' '.$result->errmsg.'.';
}
private function QueryMenus ()
{
    $access_token = empty($_GET['token']) ? '' : trim($_GET['token']);
    if (!$access_token)
    {
        echo 'operate fail,no access_token';
        return;
    }
    $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
    return file_get_contents($url);
}
private function DeleteMenus ()
{
    $access_token = empty($_GET['token']) ? '' : trim($_GET['token']);
    if (!$access_token)
    {
        echo 'operate fail,no access_token';
        return;
    }
    $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
    $result = json_decode(file_get_contents($url));
    echo $result->{'errcode'} . ' ' . $result->{'errmsg'}.'. ';
}

说明下,以上代码执行方式,先获取Access_Token后作为参数传入菜单新增、删除、查询接口进行菜单操作,如有疑问可留言。

标签: PHP 微信公众号自定义菜单 阅读: 11849
上一篇: 跨站请求伪造-CSRF防护方法 - 6736次
下一篇: 【原创】微信公众平台接口调试工具 - 15327次

网友评论

14.134.225.* 2013/11/30 23:30:32

请问下  Access_Token  这个怎么获取, 我用微信平台的在线调试工具, Access_Token  输入什么?

momy 2014/1/8 16:23:07

Access_Token是微信服务器返回的令牌,你需要输入您的密钥,发送请求给微信服务器,微信服务器计算了给你返回,你在和自己计算的对比来确认是否接口是自己开发的

210.21.206.* 2014/2/24 17:26:04

代码一模一样为什么我放到服务器上没有效果呢,求教qq1020559632

1.83.32.* 2014/7/17 22:11:14

那个$data 的参数要是从数据库查出来怎么拼成这样的格式

向右滑动解锁留言