微信公众平台自定义菜单PHP开发

转自:http://www.cnblogs.com/GmrBrian/p/3593070.html

微信公众平台自定义菜单PHP开发,微信公众平台自定义菜单是如何实现的呢?其实很简单,首先在微信公众平台升级为服务号,获取appid和appsecret,然后根据这2个参数获取access_token,在根据access_token,post一串字符到微信服务器就可以了。代码如下:

回调URL(config.php)代码:

1
2
3
4
5
6
7
8
9
define(AppId, "wx1234567890abcdef" ); //定义AppId,需要在微信公众平台申请自定义菜单后会得到
 
define(AppSecret, "1234567890abcdefghijklmnopqrstuv" ); //定义AppSecret,需要在微信公众平台申请自定义菜单后会得到
 
include ( "wechat.class.php" ); //引入微信类
 
$wechatObj  = new  Wechat(); //实例化微信类
 
$creatMenu  = $wechatObj ->creatMenu(); //创建菜单

微信类(wechat.class.php)代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class  Wechat
  {
  private  function  getAccessToken() //获取access_token
  {
  $data  = getCurl( $url ); //通过自定义函数getCurl得到https的内容
  $resultArr  = json_decode( $data , true); //转为数组
  return  $resultArr [ "access_token" ]; //获取access_token
  }
 
public  function  creatMenu() //创建菜单
  {
  $accessToken  = $this ->getAccessToken(); //获取access_token
  $menuPostString  = '{ //构造POST给微信服务器的菜单结构体
  "button" :[
  {
  "name" : "常用" ,
  "sub_button" :[
  {
  "type" : "click" ,
  "name" : "每日考勤" ,
  "key" : "1100"
  },
  {
  "type" : "click" ,
  "name" : "领卡申请" ,
  "key" : "3100"
  },
  {
  "type" : "click" ,
  "name" : "短信申请" ,
  "key" : "3200"
  },
  {
  "type" : "click" ,
  "name" : "商户曝光" ,
  "key" : "2100"
  },
  {
  "type" : "click" ,
  "name" : "商户质检" ,
  "key" : "2200"
  }
  ]
  },
  {
  "name" : "我的" ,
  "sub_button" :[
  {
  "type" : "click" ,
  "name" : "我的考勤" ,
  "key" : "1101"
  },
  {
  "type" : "click" ,
  "name" : "我的曝光" ,
  "key" : "2101"
  },
  {
  "type" : "click" ,
  "name" : "我的质检" ,
  "key" : "2201"
  },
  {
  "type" : "click" ,
  "name" : "我的锁定" ,
  "key" : "2001"
  }
  ]
  },
  {
  "name" : "数据" ,
  "sub_button" :[
  {
  "type" : "click" ,
  "name" : "消费数据" ,
  "key" : "6101"
  },
  {
  "type" : "click" ,
  "name" : "激活数据" ,
  "key" : "6102"
  },
  {
  "type" : "click" ,
  "name" : "POS手册" ,
  "key" : "4100"
  },
  {
  "type" : "click" ,
  "name" : "微信指令" ,
  "key" : "0009"
  }
  ]
  }]
  }';
  $menuPostUrl  = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $accessToken ;//POST的url
  $menu  = dataPost( $menuPostString , $menuPostUrl ); //将菜单结构体POST给微信服务器
  }
  }
 
function  getCurl( $url ){ //get https的内容
  $ch  = curl_init();
  curl_setopt( $ch , CURLOPT_URL, $url );
  curl_setopt( $ch , CURLOPT_RETURNTRANSFER,1); //不输出内容
  curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false);
  $result  =  curl_exec( $ch );
  curl_close ( $ch );
  return  $result ;
  }
 
function  dataPost( $post_string , $url ) { //POST方式提交数据
  $context  = array  ( 'http'  => array  ( 'method'  => "POST" , 'header'  => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*" , 'content'  => $post_string  ) );
  $stream_context  = stream_context_create ( $context  );
  $data  = file_get_contents  ( $url , FALSE, $stream_context  );
  return  $data ;
  }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值