码迷,mamicode.com
首页 > 其他好文 > 详细

动态创建菜单

时间:2015-06-29 21:46:31      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

  /**
   * 微信菜单发布外网
   */
  function  showwx_menu_release(){
      $wx_menu = load_mysql ("wx_menu");
      #获取一级菜单[已开启的]
      $res=$wx_menu->getAll_menu_parents();
      $data=array();
      foreach ($res[‘content‘] as $key=>$value){
          $data[]=$this->menu_arr($value[‘id‘],$value[‘menu_type‘],$value[‘menu_name‘],$value[‘mark_value_or_url‘]);
      }
      $menu_data=array(
          "button"=>$data
      );
      $data=$this->json_array($menu_data);
      $data=json_decode($this->create_menu($data),true);
      if($data["errmsg"]=="ok"){
          $this->PromptMsg = "菜单发布成功!";
      }else{
          $this->PromptMsg = "菜单发布失败!";
      }
      $this->UrlJump = "./index.php?module=wxoperation&action=wx_menu&menuId=186";
      $this->promptMsg ();
  }
  /**
   * 拼接菜单
   */
  function menu_arr($id,$type=null,$name=null,$url_key_value=null){
      #根据ID查询出开启的子类
        $wx_menu = load_mysql ("wx_menu");
        $res=$wx_menu->getAll_menu_id_chalids($id);
        if(count($res["content"])){#有子类
             $data=array(
                 "name"=>$name,
                 "sub_button"=>$this->menu_childs_arr($res["content"])
             );
        }else{ #没子类
            $types=$type==1?"view":"click";
            $url_key=$type==1?"url":"key";
          $data=array(
                      "type" => $type,
                      "name" => $name,
                      $url_key =>$url_key_value
          );
        }
      return $data;
  }
  /**
   * 处理菜单子类
   */
  function menu_childs_arr($data){
       $data_arr=array();
       foreach ($data as $key=>$value){
        $data_arr[]=$this->menu_arr($value[‘id‘],$value[‘menu_type‘],$value[‘menu_name‘],$value[‘mark_value_or_url‘]);
       }
       return $data_arr;
  }
  /**
   * 创建菜单
   */
  function create_menu($data){
      $access_token=$this->get_access_token();
      $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}"; //查询地址
      return  $this->https_request($url,$data);
  }
  /**
   * 把数组转换json  支持中午
   */
  function json_array($array) {
    #转化成JSON字符串(兼容中文)
    $str = json_encode($array);
    $search = "#\\\u([0-9a-f]{1,4}+)#ie";
    $replace = "iconv(‘UCS-2BE‘, ‘UTF-8‘, pack(‘H4‘, ‘\\1‘))";
    return preg_replace($search, $replace, $str);
  }
  /**
   *模拟一个post请求
   */
  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 json_decode($output,true);
  }
  /**
   *获取access_token
   */
  function get_access_token($appid="XXXX",$secret="XXX") {
      //请求地址
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
      $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);
      $a = curl_exec($ch);//获取地址
      curl_close($ch); //关闭
      $strjson=json_decode($a);//json解析
      $access_token = $strjson->access_token;//获取access_token
      return $access_token;
  }

 

动态创建菜单

标签:

原文地址:http://www.cnblogs.com/hgj123/p/4608317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!