您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 怎么实现微信增加菜单

2026/1/8 7:39:25发布19次查看
php实现微信增加菜单的方法:1、通过微信公众账号获取到appid和appsecret;2、通过获取凭证接口获取到access_token;3、通过“function createmenu($data){...}”方法创建自定义菜单即可。
本文操作环境:windows7系统、php7.1版、dell g3电脑
php 怎么实现微信增加菜单?
php实现创建微信自定义菜单的方法示例
具体如下:
在使用通用接口前,你需要做以下两步工作:
1.拥有一个微信公众账号,并获取到appid和appsecret(在公众平台申请内测资格,审核通过后可获得)
2.通过获取凭证接口获取到access_token
注意:
access_token是第三方访问api资源的票据;
access_token对应于公众号是全局唯一的票据,重复获取将导致上次获取的access_token失效。
访问下面这个地址(注意替换你的appid和secret):
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=appsecret
然后在浏览器能看到一下返回信息:
{"access_token":"这里就是你的access_token","expires_in":7200}
创建自定义菜单:
<?phpheader("content-type: text/html; charset=utf-8");define("access_token", "这里填入你上面获取到的access_token");//创建菜单function createmenu($data){$ch = curl_init();curl_setopt($ch, curlopt_url, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".access_token);curl_setopt($ch, curlopt_customrequest, "post");curl_setopt($ch, curlopt_ssl_verifypeer, false);curl_setopt($ch, curlopt_ssl_verifyhost, false);curl_setopt($ch, curlopt_useragent, 'mozilla/5.0 (compatible; msie 5.01; windows nt 5.0)');curl_setopt($ch, curlopt_followlocation, 1);curl_setopt($ch, curlopt_autoreferer, 1);curl_setopt($ch, curlopt_postfields, $data);curl_setopt($ch, curlopt_returntransfer, true);$tmpinfo = curl_exec($ch);if (curl_errno($ch)) { return curl_error($ch);}curl_close($ch);return $tmpinfo;}//获取菜单function getmenu(){return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".access_token);}//删除菜单function deletemenu(){return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".access_token);}$data = '{ "button":[ { "type":"click", "name":"首页", "key":"home" }, { "type":"click", "name":"简介", "key":"introduct" }, { "name":"菜单", "sub_button":[ { "type":"click", "name":"hello word", "key":"v1001_hello_world" }, { "type":"click", "name":"赞一下我们", "key":"v1001_good" }] }]}';echo createmenu($data);//echo getmenu();//echo deletemenu();
推荐学习:《php视频教程》
以上就是php 怎么实现微信增加菜单的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product