/**
*Token 获取
*PS:有效Token 需要 在微信 IP白名单 配置相应 IP地址
*/
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret
function settoken($val) {
$this->access_token = $val;
}
/**
* 创建自定义菜单
*/
function gettoken() {
$url = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
$newmenu = array(
'button' => array(
),
);
/*菜单栏端 开始(start menu)*/
$mod = M("wx");
//获取后台设置
$res = $mod->where("pid = 0 and state = 1")->order("order_by asc,id desc")->limit(3)->select(); //确定外层按钮
foreach ($res as $key => $val) {
$newmenu['button'][$key] = array(
"name" => $val['wxname'],
);
//查找是否有下级
$ppp = $mod->where("pid = {$val['id']} and state = 1")->order("order_by asc,id desc")->limit(5)->select(); //确定是否有
if (empty($ppp)) {
$newmenu['button'][$key]['sub_button'][0] = array(
"type" => "view",
"name" => $val['wxname'],
"url" => $val['url']
);
} else {
foreach ($ppp as $k => $v) {
$newmenu['button'][$key]['sub_button'][$k] = array(
"type" => "view",
"name" => $v['wxname'],
"url" => $v['url'],
);
}
}
}
/*菜单栏 结束(end menu)*/
$data = json_encode($newmenu, JSON_UNESCAPED_UNICODE);
return $this->createMenu($data);
die;
}
//创建菜单
function createMenu($data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->access_token);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
//获取菜单
function getMenu() {
if (empty($this->access_token)) {
$this->gettoken();
}
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $this->access_token);
}
//删除菜单
function deleteMenu() {
if (empty($this->access_token)) {
$this->gettoken();
}
return file_get_contents("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $this->access_token);
}
/**
*使用 该类
*/
//更新微信
function edit_savewx(){
$this->settoken($token);
$res=$this->gettoken();
$data=json_decode($res,TRUE);
if($data['errcode'] == 0 || empty($data['errcode'])){
$this->success("更新成功");
}else{
echo $res;
}
}