微信公眾平台自定義菜單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
;
}
|