wechatmenu.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Created by 上海风车教育科技有限公司.
  4. * User: 刘红伟
  5. * Date: 15-11-24
  6. * Email: 454303753@qq.com
  7. * File:wechatmenu.php
  8. */
  9. class wechatmenu{
  10. private $url;
  11. private $token;
  12. public function __construct($token){
  13. $this->token=$token;
  14. $this->setUrl();
  15. }
  16. public function setUrl(){
  17. $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->token;
  18. $this->url=$url;
  19. }
  20. public function create($data)
  21. {
  22. $http = wx_http::factory($this->url, wx_http::TYPE_CURL);
  23. $json = $http->post($this->url, $data, array(
  24. 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
  25. 'Content-Type: application/json; charset=utf-8',
  26. 'Content-Length: '. strlen($data)
  27. ));
  28. $result =json_decode($json,true);
  29. if ((isset($result['errcode']) ) OR (isset($result['errmsg'])))
  30. {
  31. $this->_errno = $result['errcode'];
  32. $this->_error = $result['errmsg'];
  33. return FALSE;
  34. }
  35. return $result;
  36. }
  37. public function setMenu($menuList){
  38. //树形排布
  39. $menuList2 = $menuList;
  40. foreach($menuList as $key=>$menu){
  41. foreach($menuList2 as $k=>$menu2){
  42. if($menu['id'] == $menu2['pid']){
  43. $menuList[$key]['sub_button'][] = $menu2;
  44. unset($menuList[$k]);
  45. }
  46. }
  47. }
  48. $typeView = 1;
  49. $typeClick = 2;
  50. $typescancode_push = 3;
  51. //处理数据
  52. foreach($menuList as $key=>$menu){
  53. //处理type和code
  54. if($menu['type'] == $typeView){
  55. $menuList[$key]['type'] = 'view';
  56. $menuList[$key]['url'] = $menu['code'];
  57. //处理URL。因为URL不能在转换JSON时被转为UNICODE
  58. $menuList[$key]['url'] = urlencode($menuList[$key]['url']);
  59. }else if($menu['type'] == $typeClick){
  60. $menuList[$key]['type'] = 'click';
  61. $menuList[$key]['key'] = $menu['code'];
  62. }else if($menu['type'] == $typescancode_push){
  63. $menuList[$key]['type'] = 'scancode_push';
  64. $menuList[$key]['key'] = $menu['code'];
  65. }
  66. unset($menuList[$key]['code']);
  67. //处理PID和ID
  68. unset($menuList[$key]['id']);
  69. unset($menuList[$key]['pid']);
  70. //处理名字。因为汉字不能在转换JSON时被转为UNICODE
  71. $menuList[$key]['name'] = urlencode($menu['name']);
  72. //处理子类菜单
  73. if(isset($menu['sub_button'])){
  74. unset($menuList[$key]['type']);
  75. foreach($menu['sub_button'] as $k=>$son){
  76. //处理type和code
  77. if($son['type'] == $typeView){
  78. $menuList[$key]['sub_button'][$k]['type'] = 'view';
  79. $menuList[$key]['sub_button'][$k]['url'] = $son['code'];
  80. $menuList[$key]['sub_button'][$k]['url'] = urlencode($menuList[$key]['sub_button'][$k]['url']);
  81. }else if($son['type'] == $typeClick){
  82. $menuList[$key]['sub_button'][$k]['type'] = 'click';
  83. $menuList[$key]['sub_button'][$k]['key'] = $son['code'];
  84. }else if($son['type'] == $typescancode_push){
  85. $menuList[$key]['sub_button'][$k]['type'] = 'scancode_push';
  86. $menuList[$key]['sub_button'][$k]['key'] = $son['code'];
  87. }
  88. unset($menuList[$key]['sub_button'][$k]['code']);
  89. //处理PID和ID
  90. unset($menuList[$key]['sub_button'][$k]['id']);
  91. unset($menuList[$key]['sub_button'][$k]['pid']);
  92. //处理名字。因为汉字不能在转换JSON时被转为UNICODE
  93. $menuList[$key]['sub_button'][$k]['name'] = urlencode($son['name']);
  94. }
  95. }
  96. }
  97. //整理格式
  98. $data = array();
  99. $menuList = array_values($menuList);
  100. $data['button'] = $menuList;
  101. //转换成JSON
  102. $data = json_encode($data);
  103. return $data = urldecode($data);
  104. }
  105. }