微信公众帐号 服务号可以使用 自定义菜单功能。之前在创建菜单时一直失败,原因是$data 格式一直没有传正确,后来终于解决了。这里先记录下 顺便封装了一个类,便于自定义菜单的管理。此类仅仅是自定义菜单的管理类,并未涉及微信自定义回复和菜单事件的代码。
代码如下
php" id="highlighter_18186">
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/**
* @author LSH 2013-09-06
*
* 微信自定义菜单的创建|查询|取消
*/
class weixinMenu {
public static $appid = null; // 申请得到的appid
public static $secret = null; // 申请得到的secret
public static $getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" ;
public static $createMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" ;
public static $selMenu = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" ;
public static $delMenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" ;
public static $opt = array (
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' ,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_AUTOREFERER =>1,
CURLOPT_RETURNTRANSFER => true
);
public $ACCESS_TOKEN = null;
/**
* 创建菜单
*/
public function create()
{
$this ->token();
$strMeau = '{
"button" :[
{
"type" : "click" ,
"name" : "菜单左" ,
"key" : "V_MENU_LEFT"
},
{
"type" : "click" ,
"name" : "菜单中" ,
"key" : "V_MENU_CENTER"
},
{
"type" : "click" ,
"name" : "菜单右" ,
"key" : "V_MENU_RIGHT"
}]
}';
$ret = $this ->HttpPost(self:: $createMenu . $this ->ACCESS_TOKEN,self:: $opt , $strMeau );
echo $ret ;
}
/**
* 查询菜单
*/
public function sel()
{
$this ->token();
$ret = $this ->HttpGet(self:: $selMenu . $this ->ACCESS_TOKEN,self:: $opt );
echo $ret ;
}
/**
* 取消菜单
*/
public function del()
{
$this ->token();
$ret = $this ->HttpGet(self:: $delMenu . $this ->ACCESS_TOKEN,self:: $opt );
echo $ret ;
}
/**
* 获取token
*/
private function token()
{
$tokenUrl = self:: $getToken . "&appid=" .self:: $appid . "&secret=" .self:: $secret ;
$ret = $this ->HttpGet( $tokenUrl ,self:: $opt );
$arrRet = json_decode( $ret ,true);
$this ->ACCESS_TOKEN = $arrRet [ 'access_token' ];
}
/**
* POST 模式
* @param string $url post 的地址
* @param array $opt post 选项
* @param array $post_data post 数据
* @return mixed
*/
private function HttpPost( $url , $opt = array (), $post_data )
{
$setopt = array (
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url ,
CURLOPT_CUSTOMREQUEST => 'POST' ,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data ,
);
if ( ! empty ( $opt ) ) {
foreach ( $opt as $key => $value ) {
$setopt [ $key ] = $value ;
}
}
$curl = curl_init( $url );
foreach ( $setopt as $key => $value ) {
curl_setopt( $curl , $key , $value );
}
$responseText = curl_exec( $curl );
curl_close( $curl );
return $responseText ;
}
/**
* GET 方式
* @param stinrg $url GET 的url
* @param array $opt GET 的选项
* @return mixed
*/
private function HttpGet( $url , $opt = array ())
{
$setopt = array (
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
);
if ( ! empty ( $opt ) ) {
foreach ( $opt as $key => $value ) {
$setopt [ $key ] = $value ;
}
}
$curl = curl_init( $url );
foreach ( $setopt as $key => $value ) {
curl_setopt( $curl , $key , $value );
}
$responseText = curl_exec( $curl );
curl_close( $curl );
return $responseText ;
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。