動態建立選單

weixin_33941350發表於2015-06-29
  /**
   * 微信選單釋出外網
   */
  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;
  }

 

相關文章