标签:style blog class code c tar
1.根据微信公众平台文档,得知创建自定义菜单有如下规则:
a.目前自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。请注意,创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来。建议测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。
注:所以最多只能创建15个功能菜单,多了就出错了!
b.
目前自定义菜单接口可实现两种类型按钮,如下:
click: 用户点击click类型按钮后,微信服务器会通过消息接口推送消息类型为event 的结构给开发者(参考消息接口指南),并且带上按钮中开发者填写的key值,开发者可以通过自定义的key值与用户进行交互; view: 用户点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的url值 (即网页链接),达到打开网页的目的,建议与网页授权获取用户基本信息接口结合,获得用户的登入个人信息。
接口调用请求说明
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
2.根据文档提供的规则和接口调用说明,封装成一个类编写代码如下
class WeChatCreateMenu
{
//判断token是否过期,如果过期重新获取,不然直接从数据库取
public function Get_access_token($appid,$secret){
$db = DBManage::getInstance();
$db->ConnDB();
$sql = "select * from t_token";
if($db-> GetEffectRows($sql) > 0)
{
$sql = "select * from t_token where TokenFlag=‘sd‘";
$res = $db->GetData($sql);
while( $obj = mysqli_fetch_object($res))
{
if($obj->TokenTime-60 > time()) //Token还没过期
{
return $obj->Token;
}
else //过期
{
break;
}
}
}
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$json=self::DoCurlGetRequest($url);
$retData=json_decode($json,true);
if(!$retData)
{
return false;
}
$token = $retData[‘access_token‘];
$expire = $retData[‘expires_in‘];
$oldTime = time() + $expire;
$sql = "select * from t_token";
if($db-> GetEffectRows($sql) == 0) //无数据
{
$sql = "insert into t_token values(‘sd‘,‘$token‘,‘$oldTime‘)";
$db->ExecSql($sql);
}
else //更新 数据
{
$sql = "update t_token set Token=‘$token‘,TokenTime=‘$oldTime‘ where TokenFlag=‘sd‘";
$db->ExecSql($sql);
}
//file_put_contents("debug.txt", "token====".$token.PHP_EOL,FILE_APPEND);
return $token;
}
//因为url是https 所有请求不能用file_get_contents,用curl请求json 数据
private function DoCurlGetRequest($url){
$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);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function DoCurlPostRequest($url, $jsonData){
$con = curl_init((string)$url);
curl_setopt($con,CURLOPT_HEADER, false);
curl_setopt($con, CURLOPT_POSTFIELDS,$jsonData);
curl_setopt($con, CURLOPT_POST, true);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
curl_setopt($con,CURLOPT_SSL_VERIFYPEER,false); //略过证书验证
$result = curl_exec($con) ;
if(curl_errno($con))
{
file_put_contents("tmp.txt", curl_errno($con).PHP_EOL,FILE_APPEND);
}
return $result;
}
public function CreateMenu()
{
$token = $this->Get_access_token("wxa5fd3441ffa34","a49921c7d71b57850c57d1f589");
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$token; //自定义菜单接口
$menuData = array(
‘button‘ => array(
array(
‘name‘ => ‘走进窗骑‘,
‘sub_button‘ => array(
array(
‘type‘ =>‘click‘,
‘name‘ => ‘微官网‘,
‘key‘ => ‘sdwgw‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘全品牌推广‘,
‘url‘ => ‘http://128.88.44.11/WXProject/DataHtml/html/qpptg.html‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘3D影视包装‘,
‘url‘ => ‘http://128.88,44,11/WXProject/DataHtml/html/3d.html‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘互动体验‘,
‘url‘ => ‘http://128.88.44.11/WXProject/DataHtml/html/hudong.html‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘合作客户‘,
‘url‘ => ‘http://128.88.44.11/WXProject/DataHtml/html/partner.html‘
)
)
),
array(
‘name‘ => ‘微楼书‘,
‘sub_button‘ =>array(
array(
‘type‘ =>‘click‘,
‘name‘ => ‘项目概况‘,
‘key‘ => ‘xmgk‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘区位配套‘,
‘url‘ => ‘http://128.88.44.11/WXProject/DataHtml/qwpt.html‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘景观鉴赏‘,
‘url‘ => ‘http://128.88.44.11/WXProject/WXPicBrowser/jgjd.php‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘户型展示‘,
‘url‘ => ‘http://128.88.44.11/WXProject/WXPicBrowser/jdhx.php‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘3D视频‘,
‘url‘ => ‘http://128.88.44.11/WXProject/DataHtml/video.html‘
)
)
),
array(
‘name‘ => "互动体验",
‘sub_button‘ => array(
array(
‘type‘ =>‘click‘,
‘name‘ => ‘会员中心‘,
‘key‘ => ‘hyzx‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘看房预约‘,
‘url‘ => ‘http://128.88.44.11/WXProject/DataPHP/seehouse.php‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘智能问答‘,
‘url‘ => ‘http://128.88.44.11/WXProject/DataPHP/test.php‘
),
array(
‘type‘ =>‘view‘,
‘name‘ => ‘一键功能‘,
‘url‘ => ‘http://128.88.44.11/WXProject/onetouchdial.html#mp.weixin.qq.com‘
),
array(
‘type‘ =>‘click‘,
‘name‘ => ‘刮刮乐‘,
‘key‘ => ‘ggl‘
)
)
)
)
);
$data = JSON($menuData);
$this->DoCurlPostRequest($url,$data); //以https发送post请求
}
}
写完上面的类,直接在index.php,调用就行了;然后按前面几篇介绍的方法传到sae上面,运行测试
3.大概思路:根据微信的文档说明,首先按规则创建好菜单,然后按规定写好创建自定义菜单接口,接着用https发送post请求,微信服务器收到请求,判断发送过来的接口没错;微信服务器发送创建好的菜单到每一位微信用户的客户端显示。
【微信公众平台开发】创建自定义菜单(四),布布扣,bubuko.com
标签:style blog class code c tar
原文地址:http://blog.csdn.net/qivan/article/details/25607025