关闭

微信公众平台消息接口开发(9)骑行西藏自定义菜单

标签: 微信公众平台微信
1251人阅读 评论(0) 收藏 举报
分类:

1. 申请成为服务号(新申请订阅号无法开发申请菜单)

2. 申请自定义菜单,得到appid和appsecert

3. 用appid和appsecert获得access token
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

{"access_token":"N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g","expires_in":7200}

 
4. 将菜单组织成结构,通过post提交给接口

复制代码
$xjson = '{
"button":[
{
"name":"天气预报",
"sub_button":[
{
"type":"click",
"name":"北京天气",
"key":"天气北京"
},
{
"type":"click",
"name":"上海天气",
"key":"天气上海"
},
{
"type":"click",
"name":"广州天气",
"key":"天气广州"
},
{
"type":"click",
"name":"深圳天气",
"key":"天气深圳"
},
{
"type":"view",
"name":"本地天气",
"url":"http://m.hao123.com/a/tianqi"
}]

},
{
"name":"休闲娱乐",
"sub_button":[
{
"type":"click",
"name":"移动电影",
"key":"电影"
},
{
"type":"click",
"name":"趣味游戏",
"key":"游戏"
},
{
"type":"click",
"name":"讲个笑话",
"key":"笑话"
}]
}]
}';

$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=p0tEu5tgSgQmZP4UKa1Bvd2Y9BL-03Uz2FXj2j-LX9hFuHt49ExIOyvKtNi4DWycl73Vs2SIsl1I6QqEpzyQpBNK2vRFHZKk1fS6HsXMWn522cvoOAhGX13aSj4zmoU5sQnX-FEpD36XwQJqw8IXkQ";
$result = vpost($url,$xjson);
var_dump($result);
 
function vpost($url,$data){ // 模拟提交数据函数
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
    // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    // curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
       echo 'Errno'.curl_error($curl);//捕抓异常
    }
    curl_close($curl); // 关闭CURL会话
    return $tmpInfo; // 返回数据
}
复制代码

 


5. 在消息接口中处理event事件,其中的click代表菜单点击,通过响应菜单结构中的key值回应消息

复制代码
private function receiveEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
            case "subscribe":
                $contentStr[] = array("Title" =>"欢迎关注方倍工作室", "Description" =>"点击图片关注或者微信搜索方倍工作室", "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/pondbaystudio");
            case "unsubscribe":
                $contentStr = "";
                break;
            case "CLICK":
                switch ($object->EventKey)
                {
                    case "company":
                        $contentStr[] = array("Title" =>"公司简介", "Description" =>"方倍工作室提供移动互联网相关的产品及服务,包括新浪微博应用、微信公众平台接口、手机版网站等", "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/pondbaystudio");
                        break;
                    default:
                        $contentStr[] = array("Title" =>"默认菜单回复", "Description" =>"您正在使用的是方倍工作室的自定义菜单测试接口", "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/pondbaystudio");
                        break;
                }
                break;
            default:
                $contentStr = "receive a new event: ".$object->Event;
                break;      

       }
        return $contentStr;
    }
复制代码

 

 

 

.net代码:

复制代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=HI3brcIDTXHIxpDoxJfYSGd9Yx-OgQNlEmJ48I80SV8tD0zpCzA1yZ069E1GTjz6-3aAZHfrYHIZKdgsRvWlyv7OR5PJjrq3Zy-MgSyZ4Xhuvz2qJr3mjGmZ0cjYQ0kh";
            string param = "{\"button\":[{\"type\":\"click\",\"name\":\"今日歌曲\",\"key\":\"V1001_TODAY_MUSIC\"},{\"name\":\"菜单\",\"sub_button\":[{\"type\":\"click\",\"name\":\"helloword\",\"key\":\"V1001_HELLO_WORLD\"},{\"type\":\"click\",\"name\":\"赞一下我们\",\"key\":\"V1001_GOOD\"}]}]}";
            string result = postWebReq(url, param, Encoding.UTF8);
            
        }


        static string postWebReq(string postUrl, string paramData, Encoding dataEncode)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(paramData);
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.ContentLength = byteArray.Length;

                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                newStream.Close();

                HttpWebResponse reponse = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(reponse.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                
                sr.Close();
                reponse.Close();
                newStream.Close();


            }
            catch (Exception ex)
            {
                
            }
            return ret;
        }

    }
}
复制代码

 

  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22886次
    • 积分:333
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:15篇
    • 译文:6篇
    • 评论:2条
    文章分类
    最新评论