php微信开发之自定义菜单完整流程

2025-05-29 0 67

一、自定义菜单概述

自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。开启自定义菜单后,公众号界面如图所示:

php微信开发之自定义菜单完整流程

二、申请自定义菜单

个人订阅号使用微博认证、企业订阅号通过微信认证;可以申请到自定义菜单资格

服务号默认有菜单权限。

三、获得appid 和appsecert

appid和appsecret在开发者中心-开发者id中,可以找到。

php微信开发之自定义菜单完整流程

四、获得access token

用appid和appsecert获得access token,接口为

https://api.weixin.qq.com/cgi-bi … mp;secret=appsecret

程序实现如下

?

1

2

3

4

5

6

7

8

9

10

11

12
$appid = "";

$appsecret = "";

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";

$ch = curl_init();

curl_setopt($ch, curlopt_url, $url);

curl_setopt($ch, curlopt_ssl_verifypeer, false);

curl_setopt($ch, curlopt_ssl_verifyhost, false);

curl_setopt($ch, curlopt_returntransfer, 1);

$output = curl_exec($ch);

curl_close($ch);

$jsoninfo = json_decode($output, true);

$access_token = $jsoninfo["access_token"];

你也可以直接在浏览器地址栏中,拼接出地址,执行后,获得如下数据

复制代码 代码如下:

{"access_token":"n2l7kxa084wvelonyjkj_trabmccvy_ukmpuuzlrq0ea2ynp3iz6esurrg0bhar_viswd50vdupky5ng43d1gbm-olt2krmxosve08rfed9lvk9lmgung9kpikkgzejif8jv2m9ffhf8bnna-yqh3g",

复制代码 代码如下:

"expires_in":7200}

参数说明如下

php微信开发之自定义菜单完整流程

其中的
n2l7kxa084wvelonyjkj_trabmccvy_ukmpuuzlrq0ea2ynp3iz6esurrg0bhar_viswd50vdupky5ng43d1gbm-olt2krmxosve08rfed9lvk9lmgung9kpikkgzejif8jv2m9ffhf8bnna-yqh3g
就是access token。

或者使用官方的接口调试工具,地址为:
https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%e8%87%aa%e5%ae%9a%e4%b9%89%e8%8f%9c%e5%8d%95&form=%e8%87%aa%e5%ae%9a%e4%b9%89%e8%8f%9c%e5%8d%95%e5%88%9b%e5%bb%ba%e6%8e%a5%e5%8f%a3%20/menu/create

使用网页调试工具调试自定义菜单接口

php微信开发之自定义菜单完整流程

点击检查问题得,得到

php微信开发之自定义菜单完整流程

这样也获得了access token

五、组织菜单内容

目前自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“…”代 替。请注意,创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。建议测试时可以尝试取消关注公众账号后再次关注,则可以看到创 建后的效果。

目前自定义菜单接口可实现两种类型按钮,如下:

click:
用户点击click类型按钮后,微信服务器会通过消息接口推送消息类型为event 的结构给开发者(参考消息接口指南),并且带上按钮中开发者填写的key值,开发者可以通过自定义的key值与用户进行交互;
view:
用户点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的url值 (即网页链接),达到打开网页的目的,建议与网页授权获取用户基本信息接口结合,获得用户的登入个人信息。

接口调用请求说明

http请求方式:post(请使用https协议) https://api.weixin.qq.com/cgi-bi … _token=access_token

请求示例

?

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
{

"button":[

{

"type":"click",

"name":"今日歌曲",

"key":"v1001_today_music"

},

{

"type":"click",

"name":"歌手简介",

"key":"v1001_today_singer"

},

{

"name":"菜单",

"sub_button":[

{

"type":"view",

"name":"搜索",

"url":"http://www.soso.com/"

},

{

"type":"view",

"name":"视频",

"url":"http://v.qq.com/"

},

{

"type":"click",

"name":"赞一下我们",

"key":"v1001_good"

}]

}]

}

参数说明

php微信开发之自定义菜单完整流程

返回结果

正确时的返回json数据包如下:

{"errcode":0,"errmsg":"ok"}

错误时的返回json数据包如下(示例为无效菜单名长度):

{"errcode":40018,"errmsg":"invalid button name size"}

六、提交菜单内容给服务器

菜单的json结构为

?

1

2

3

4

5

6
{"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":"company"},

{"type":"click","name":"趣味游戏","key":"游戏"}, {"type":"click","name":"讲个笑话","key":"笑话"}]}]}

将以下代码保存为menu.php,并且在浏览器中运行该文件(比如 http://127.0.0.1/menu.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
php

$access_token = "";

$jsonmenu = '{

"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":"company"

},

{

"type":"click",

"name":"趣味游戏",

"key":"游戏"

},

{

"type":"click",

"name":"讲个笑话",

"key":"笑话"

}]

}]

}';

$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;

$result = https_request($url, $jsonmenu);

var_dump($result);

function https_request($url,$data = null){

$curl = curl_init();

curl_setopt($curl, curlopt_url, $url);

curl_setopt($curl, curlopt_ssl_verifypeer, false);

curl_setopt($curl, curlopt_ssl_verifyhost, false);

if (!empty($data)){

curl_setopt($curl, curlopt_post, 1);

curl_setopt($curl, curlopt_postfields, $data);

}

curl_setopt($curl, curlopt_returntransfer, 1);

$output = curl_exec($curl);

curl_close($curl);

return $output;

}

?>

或者使用官方的调试接口 使用网页调试工具调试该接口

php微信开发之自定义菜单完整流程

php微信开发之自定义菜单完整流程

提交成功后,重新关注后即可看到菜单。菜单效果类似如下:

php微信开发之自定义菜单完整流程

七、响应菜单点击事件

在消息接口中处理event事件,其中的click代表菜单点击,通过响应菜单结构中的key值回应消息,view事件无须响应,将直接跳转过去

?

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
define("token", "weixin");

$wechatobj = new wechatcallbackapitest();

if (!isset($_get['echostr'])) {

$wechatobj->responsemsg();

}else{

$wechatobj->valid();

}

class wechatcallbackapitest

{

public function valid()

{

$echostr = $_get["echostr"];

if($this->checksignature()){

echo $echostr;

exit;

}

}

private function checksignature()

{

$signature = $_get["signature"];

$timestamp = $_get["timestamp"];

$nonce = $_get["nonce"];

$token = token;

$tmparr = array($token, $timestamp, $nonce);

sort($tmparr);

$tmpstr = implode( $tmparr );

$tmpstr = sha1( $tmpstr );

if( $tmpstr == $signature ){

return true;

}else{

return false;

}

}

public function responsemsg()

{

$poststr = $globals["http_raw_post_data"];

if (!empty($poststr)){

$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);

$rx_type = trim($postobj->msgtype);

switch ($rx_type)

{

case "text":

$resultstr = $this->receivetext($postobj);

break;

case "event":

$resultstr = $this->receiveevent($postobj);

break;

default:

$resultstr = "";

break;

}

echo $resultstr;

}else {

echo "";

exit;

}

}

private function receivetext($object)

{

$funcflag = 0;

$contentstr = "你发送的内容为:".$object->content;

$resultstr = $this->transmittext($object, $contentstr, $funcflag);

return $resultstr;

}

private function receiveevent($object)

{

$contentstr = "";

switch ($object->event)

{

case "subscribe":

$contentstr = "欢迎洋洋博客";

case "unsubscribe":

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" =>"您正在使用的是<span style="font-family: arial, helvetica, sans-serif;">洋洋的博客</span><span style="font-family: arial, helvetica, sans-serif;">", </span>

"picurl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg",

"url" =>"weixin://addfriend/pondbaystudio");

break;

}

break;

default:

break;

}

if (is_array($contentstr)){

$resultstr = $this->transmitnews($object, $contentstr);

}else{

$resultstr = $this->transmittext($object, $contentstr);

}

return $resultstr;

}

private function transmittext($object, $content, $funcflag = 0)

{

$texttpl = "<xml>

<tousername><![cdata[%s]]></tousername>

<fromusername><![cdata[%s]]></fromusername>

<createtime>%s</createtime>

<msgtype><![cdata[text]]></msgtype>

<content><![cdata[%s]]></content>

<funcflag>%d</funcflag>

</xml>";

$resultstr = sprintf($texttpl, $object->fromusername, $object->tousername, time(), $content, $funcflag);

return $resultstr;

}

private function transmitnews($object, $arr_item, $funcflag = 0)

{

//首条标题28字,其他标题39字

if(!is_array($arr_item))

return;

$itemtpl = " <item>

<title><![cdata[%s]]></title>

<description><![cdata[%s]]></description>

<picurl><![cdata[%s]]></picurl>

<url><![cdata[%s]]></url>

</item>

";

$item_str = "";

foreach ($arr_item as $item)

$item_str .= sprintf($itemtpl, $item['title'], $item['description'], $item['picurl'], $item['url']);

$newstpl = "<xml>

<tousername><![cdata[%s]]></tousername>

<fromusername><![cdata[%s]]></fromusername>

<createtime>%s</createtime>

<msgtype><![cdata[news]]></msgtype>

<content><![cdata[]]></content>

<articlecount>%s</articlecount>

<articles>

$item_str</articles>

<funcflag>%s</funcflag>

</xml>";

$resultstr = sprintf($newstpl, $object->fromusername, $object->tousername, time(), count($arr_item), $funcflag);

return $resultstr;

}

}

?>

八、菜单中获取openid

由于菜单中只能填写固定的url地址,对于想要菜单中获取用户的openid的情况,可以使用oauth2.0授权的方式来实现。

url中填写的地址为一个固定的回调地址。原理方法可以参考 微信公众平台开发(99) 自定义菜单获取openid

?

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
<?php

/*

洋洋的博客

*/

define("token", "weixin");

$wechatobj = new wechatcallbackapitest();

if (isset($_get['echostr'])) {

$wechatobj->valid();

}else{

$wechatobj->responsemsg();

}

class wechatcallbackapitest

{

public function valid()

{

$echostr = $_get["echostr"];

if($this->checksignature()){

header('content-type:text');

echo $echostr;

exit;

}

}

private function checksignature()

{

$signature = $_get["signature"];

$timestamp = $_get["timestamp"];

$nonce = $_get["nonce"];

$token = token;

$tmparr = array($token, $timestamp, $nonce);

sort($tmparr, sort_string);

$tmpstr = implode( $tmparr );

$tmpstr = sha1( $tmpstr );

if( $tmpstr == $signature ){

return true;

}else{

return false;

}

}

public function responsemsg()

{

$poststr = $globals["http_raw_post_data"];

if (!empty($poststr)){

$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);

$fromusername = $postobj->fromusername;

$tousername = $postobj->tousername;

$keyword = trim($postobj->content);

$time = time();

$texttpl = "<xml>

<tousername><![cdata[%s]]></tousername>

<fromusername><![cdata[%s]]></fromusername>

<createtime>%s</createtime>

<msgtype><![cdata[%s]]></msgtype>

<content><![cdata[%s]]></content>

<funcflag>0</funcflag>

</xml>";

if($keyword == "?" || $keyword == "?")

{

$msgtype = "text";

$contentstr = '当前时间是:'.date("y-m-d h:i:s",time());

$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);

echo $resultstr;

}

}else{

echo "";

exit;

}

}

}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php微信开发之自定义菜单完整流程 https://www.kuaiidc.com/96907.html

相关文章

发表评论
暂无评论