微信公众平台开发 微信公众平台开发模式 企业微信公众平台 自定义菜单 开发教程 

作者:方倍工作室 

1. 申请成为服务号

2. 申请自定义菜单,得到appid和appsecert

3. 用appid和appsecert获得access token

​https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET​



{"access_token":"N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g","expires_in":7200}


 

4. 将菜单组织成结构,通过post提交给接口

 



$xjson = '{
"button":[
{
"name":"天气预报",
"sub_button":[
{
"type":"click",
"name":"北京天气",
"key":"天气北京"
},
{
"type":"click",
"name":"上海天气",
"key":"天气上海"
},
{
"type":"click",
"name":"广州天气",
"key":"天气广州"
},
{
"type":"click",
"name":"深圳天气",
"key":"天气深圳"
},
{
"type":"view",
"name":"本地天气",
"url":"http://m.hao123.com/a/tianqi"
}]
},
{
"name":"生活服务",
"sub_button":[
{
"type":"click",
"name":"话费充值",
"key":"活动充值"
},
{
"type":"click",
"name":"彩票购买",
"key":"活动彩票"
},
{
"type":"click",
"name":"充值中奖",
"key":"活动中奖"
}]
},
{
"name":"关于方倍",
"sub_button":[
{
"type":"click",
"name":"公司简介",
"key":"company"
}]
}]
}';

$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=p0tEu5tgSgQmZP4UKa1Bvd2Y9BL-03Uz2FXj2j-LX9hFuHt49ExIOyvKtNi4DWycl73Vs2SIsl1I6QqEpzyQpBNK2vRFHZKk1fS6HsXMWn522cvoOAhGX13aSj4zmoU5sQnX-FEpD36XwQJqw8IXkQ";
$result = vpost($url,$xjson);
var_dump($result);

function vpost($url,$data){ // 模拟提交数据函数
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
// curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
// curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据
}


 

5. 在消息接口中处理event事件,其中的click代表菜单点击,通过响应菜单结构中的key值回应消息



private function receiveEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr[] = array("Title" =>"欢迎关注方倍工作室", "Description" =>"点击图片关注或者搜索号码beancube", "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/beancube");
case "unsubscribe":
$contentStr = "";
break;
case "CLICK":
switch ($object->EventKey)
{
case "company":
$contentStr[] = array("Title" =>"公司简介", "Description" =>"方倍工作室提供移动互联网相关的产品及服务,包括新浪微博应用、微信公众平台接口、手机版网站等", "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/beancube");
break;
default:
$contentStr[] = array("Title" =>"默认菜单回复", "Description" =>"您正在使用的是方倍工作室的自定义菜单测试接口", "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"weixin://addfriend/beancube");
break;
}
break;
default:
$contentStr = "receive a new event: ".$object->Event;
break;

}
return $contentStr;
}


 

 

 

=============================================================

 

欢迎关注方倍工作室微信,了解我们及行业的最新动态

微信公众平台开发(58)自定义菜单_json