服务器之家

服务器之家 > 正文

PHP实现微信公众号企业号自定义菜单接口示例

时间:2021-06-20 16:53     来源/作者:027099

本文实例讲述了PHP实现微信公众号企业号自定义菜单接口。分享给大家供大家参考,具体如下:

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

微信类(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
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
<?
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":"view",
        "name":"分销A型",
        "url":"http://www.yourwebname.com/fenxiao/jianjie/soft.html"
      },
      {
        "type":"view",
        "name":"分销B型",
        "url":"http://www.yourwebname.com/fenxiaob/jianjie/soft.html"
      },{
        "type":"view",
        "name":"地接批发",
        "url":"http://www.yourwebname.com/dijie/jianjie/soft.html"
      },{
        "type":"view",
        "name":"精简组团",
        "url":"http://www.yourwebname.com/zutuan/jianjie/soft.html"
      },{
        "type":"view",
        "name":"直客网站",
        "url":"http://www.yourwebname.com/tripal/jianjie/soft.html"
      }]
    },
   {
      "name":"申请试用",
      "sub_button":[
      {
        "type":"click",
        "name":"分销A型",
        "key":"fxa"
      },
      {
        "type":"click",
        "name":"分销B型",
        "key":"fxb"
      },
      {
        "type":"click",
        "name":"地接批发",
        "key":"dj"
      },
      {
        "type":"click",
        "name":"精简组团",
        "key":"zutuan"
      },
      {
        "type":"click",
        "name":"直客网站",
        "key":"zhike"
      }
      ]
    },
      {
      "name":"博纵在线",
      "sub_button":[
      {
        "type":"view",
        "name":"企业介绍",
        "url":"http://www.yourwebname.com/about.html"
      },
      {
        "type":"view",
        "name":"公司新闻",
        "url":"http://www.yourwebname.com/news/company/"
      },
      {
        "type":"view",
        "name":"联系我们",
        "url":"http://www.yourwebname.com/contact.html"
      }
      ]
    }
    ]
 }';
 $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;
}
?>

希望本文所述对大家PHP程序设计有所帮助。

原文链接:http://blog.csdn.net/k8080880/article/details/40372841

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部