微信公众号中,关于自定义菜单的创建,一般的公众号都是会用到的
官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
相关代码:
class LibWechat extends Base
{
private $appid = ''; //自己appid
private $appsecret = ''; //自己appsecret
private $_access_token; //微信网页授权
public $error;
public $return_url; //用户同意授权跳转页面
public $path; //用户授权前所在页面(用于授权后回调)
public $code; //授权code
public $open_id; //openid,用户唯一凭证
/**
* curl 请求
*/
public function requestUrl($url, $data = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //禁止 cURL 验证对等证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 为SSL不检查名称
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/*
* 获取普通通用token
*/
public function getToken()
{
if (empty($_SESSION['accessToken'])) {
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appid . '&secret=' . $this->appsecret;
$result = json_decode($this->requestUrl($url), true);
if (empty($result['access_token'])) {
$this->error = $result['errorMsg'];
return false;
}
$_SESSION['accessToken'] = $result['access_token'];
}
$this->access_token = $_SESSION['accessToken'];
}
/*
* 自定义菜单创建
*/
public function takeMenu()
{
$openid = $_SESSION['openid'];
$user['id'] = M('user')->where(array('openid' => $openid))->find();
if (empty($_SESSION['accessToken'])) {
$this->getToken();
}
$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $_SESSION['accessToken'];
$menu_array = '{
"button":[{
"name":"公司介绍",
"sub_button":[
{
"type":"view",
"name":"公司简介",
"url":"https://mp.weixin.qq.com"
}
]
}],
"button":[{
"name":"课程学习",
"sub_button":[
{
"type":"view",
"name":"百度",
"url":"http://baidu.com"
},
{
"type":"view",
"name":"微信授权",
"url":"https://blog.csdn.net/zhangzhangdan/article/details/80760624"
},
{
"type":"view",
"name":"微信获取带参数二维码",
"url":"https://blog.csdn.net/zhangzhangdan/article/details/80761564"
}]
}]
}';
$result = json_decode($this->requestUrl($url, $menu_array), true);
if($result['errcode'] == '0'){
$this->error = '菜单生成失败';
}
return $result;
}
这个是点击跳转路径的,你要是需要别的模式,你可以看看官方文档
备注:url路径必须是以http开头的,不能为www