微信公众号 自定义菜单
经常在弄微信的时候用到自定义菜单,今天我先大概发一个例子,方便日后使用
控制器
public function index(){
//查询微信公众号配置信息
$wx_con = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."weixin_account");
$appid = $wx_con['authorizer_appid'];
$secret = $wx_con['appsecret'];
$access_token = get_accessToken($appid,$secret);
$data = array(
'button' => array('name'=>'会生活','sub_button'=>array(array('type'=>'view','name'=>'最新生活精选','url'=>'http://www.huigood.net/wap/index.php?ctl=main'),array('type'=>'view','name'=>'爱旅行','url'=>'http://www.huigood.net/index.php?ctl=tuan&cid=11'),array('type'=>'view','name'=>'约娱乐','url'=>'http://www.huigood.net/index.php?ctl=tuan&cid=9'),array('type'=>'view','name'=>'找美食','url'=>'http://www.huigood.net/index.php?ctl=tuan&cid=8'),array('type'=>'view','name'=>'附近优惠','url'=>'http://www.huigood.net/wap/index.php?ctl=stores'))),
array('name'=>'会优惠','sub_button'=>array(array('type'=>'view','name'=>'优惠买单','url'=>'http://www.huigood.net/wap/index.php?ctl=stores_pay'),array('type'=>'view','name'=>'热门活动','url'=>'http://www.huigood.net/wap/index.php?ctl=events'),array('type'=>'view','name'=>'积分兑换','url'=>'http://www.huigood.net/wap/index.php?ctl=scores_index'),array('type'=>'view','name'=>'领券中心','url'=>'http://www.huigood.net/wap/index.php?ctl=youhuis'))),
array('name'=>'我的会购','sub_button'=>array(array('type'=>'view','name'=>'商户登陆','url'=>'http://www.huigood.net/wap/biz.php'),array('type'=>'view','name'=>'个人中心','url'=>'http://www.huigood.net/wap/index.php?ctl=user_center'),array('type'=>'view','name'=>'商城首页','url'=>'http://www.huigood.net/wap/index.php')))
);
//$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$data = '
{
"button": [
{
"name": "会生活",
"sub_button": [
{
"type": "view",
"name": "最新生活精选",
"url" : "http://www.huigood.net/wap/index.php?ctl=main"
},
{
"type": "view",
"name": "爱旅行",
"url" : "http://www.huigood.net/index.php?ctl=tuan&cid=11"
},
{
"type": "view",
"name": "约娱乐",
"url" : "http://www.huigood.net/index.php?ctl=tuan&cid=9"
},
{
"type": "view",
"name": "找美食",
"url" : "http://www.huigood.net/index.php?ctl=tuan&cid=8"
},
{
"type": "view",
"name": "附近优惠",
"url" : "http://www.huigood.net/wap/index.php?ctl=stores"
}
]
},
{
"name": "会优惠",
"sub_button": [
{
"type": "view",
"name": "优惠买单",
"url" : "http://www.huigood.net/wap/index.php?ctl=stores_pay"
},
{
"type": "view",
"name": "热门活动",
"url" : "http://www.huigood.net/wap/index.php?ctl=events"
},
{
"type": "view",
"name": "积分兑换",
"url" : "http://www.huigood.net/wap/index.php?ctl=scores_index"
},
{
"type": "view",
"name": "领券中心",
"url" : "http://www.huigood.net/wap/index.php?ctl=youhuis"
}
]
},
{
"name": "我的会购",
"sub_button": [
{
"type": "view",
"name": "商户登陆",
"url" : "http://www.huigood.net/wap/biz.php"
},
{
"type": "view",
"name": "个人中心",
"url" : "http://www.huigood.net/wap/index.php?ctl=user_center"
},
{
"type": "view",
"name": "商城首页",
"url" : "http://www.huigood.net/wap/index.php"
}
]
}
]
}
';
//发送post请求
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
$post = https_post($url,$data);
var_dump($post);
}
用到的https_post方法
/**
* 发送post请求
* @param string $url 请求地址
* @param array $data post键值对数据
* @return string
*/
function https_post($url, $data) {
$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 $output;
}

浙公网安备 33010602011771号