微信公众平台自定义菜单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
{
$url
= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .AppId. "&secret=" .AppSecret;
$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 ;
}
|