设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 195|回复: 0

微信公众平台自定义菜单PHP开发

[复制链接]
发表于 2014-4-17 13:15:00 | 显示全部楼层 |阅读模式
微信公众平台自定义菜单PHP开发,微信公众平台自定义菜单是如何实现的呢?其实很简单,首先在微信公众平台升级为服务号,获取appid和appsecret,然后根据这2个参数获取access_token,在根据access_token,post一串字符到微信服务器就可以了。代码如下:
回调URL(config.php)代码:
  1. define(AppId, "wx1234567890abcdef");//定义AppId,需要在微信公众平台申请自定义菜单后会得到

  2. define(AppSecret, "1234567890abcdefghijklmnopqrstuv");//定义AppSecret,需要在微信公众平台申请自定义菜单后会得到

  3. include("wechat.class.php");//引入微信类

  4. $wechatObj = new Wechat();//实例化微信类

  5. $creatMenu = $wechatObj->creatMenu();//创建菜单
复制代码

微信类(wechat.class.php)代码
  1. class Wechat
  2. {
  3. private function getAccessToken() //获取access_token
  4. {
  5. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".AppId."&secret=".AppSecret;
  6. $data = getCurl($url);//通过自定义函数getCurl得到https的内容
  7. $resultArr = json_decode($data, true);//转为数组
  8. return $resultArr["access_token"];//获取access_token
  9. }

  10. public function creatMenu()//创建菜单
  11. {
  12. $accessToken = $this->getAccessToken();//获取access_token
  13. $menuPostString = '{//构造POST给微信服务器的菜单结构体
  14. "button":[
  15. {
  16. "name":"常用",
  17. "sub_button":[
  18. {
  19. "type":"click",
  20. "name":"每日考勤",
  21. "key":"1100"
  22. },
  23. {
  24. "type":"click",
  25. "name":"领卡申请",
  26. "key":"3100"
  27. },
  28. {
  29. "type":"click",
  30. "name":"短信申请",
  31. "key":"3200"
  32. },
  33. {
  34. "type":"click",
  35. "name":"商户曝光",
  36. "key":"2100"
  37. },
  38. {
  39. "type":"click",
  40. "name":"商户质检",
  41. "key":"2200"
  42. }
  43. ]
  44. },
  45. {
  46. "name":"我的",
  47. "sub_button":[
  48. {
  49. "type":"click",
  50. "name":"我的考勤",
  51. "key":"1101"
  52. },
  53. {
  54. "type":"click",
  55. "name":"我的曝光",
  56. "key":"2101"
  57. },
  58. {
  59. "type":"click",
  60. "name":"我的质检",
  61. "key":"2201"
  62. },
  63. {
  64. "type":"click",
  65. "name":"我的锁定",
  66. "key":"2001"
  67. }
  68. ]
  69. },
  70. {
  71. "name":"数据",
  72. "sub_button":[
  73. {
  74. "type":"click",
  75. "name":"消费数据",
  76. "key":"6101"
  77. },
  78. {
  79. "type":"click",
  80. "name":"激活数据",
  81. "key":"6102"
  82. },
  83. {
  84. "type":"click",
  85. "name":"POS手册",
  86. "key":"4100"
  87. },
  88. {
  89. "type":"click",
  90. "name":"微信指令",
  91. "key":"0009"
  92. }
  93. ]
  94. }]
  95. }';
  96. $menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;//POST的url
  97. $menu = dataPost($menuPostString, $menuPostUrl);//将菜单结构体POST给微信服务器
  98. }
  99. }

  100. function getCurl($url){//get https的内容
  101. $ch = curl_init();
  102. curl_setopt($ch, CURLOPT_URL,$url);
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
  104. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  105. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  106. $result =  curl_exec($ch);
  107. curl_close ($ch);
  108. return $result;
  109. }

  110. function dataPost($post_string, $url) {//POST方式提交数据
  111. $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 ) );
  112. $stream_context = stream_context_create ( $context );
  113. $data = file_get_contents ( $url, FALSE, $stream_context );
  114. return $data;
  115. }
复制代码



您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 13:51 , Processed in 0.014395 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表