/**
* 微信菜单发布外网
*/
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;
}
转载于:https://www.cnblogs.com/hgj123/p/4608317.html
相关资源:JAVA上百实例源码以及开源项目