获取凭证
接口说明
在使用通用接口前,你需要做以下两步工作:
1.拥有一个微信公众账号,并获取到appid和appsecret(在公众平台申请自定义菜单功能,开启开发者模式,填写URL与Token后获得)
2.通过获取凭证接口获取到access_token
注意:
access_token是第三方访问api资源的票据;
access_token对应于公众号是全局唯一的票据,重复获取将导致上次获取的access_token失效。
请求说明
http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数说明
参数 是否必须 说明
grant_type 是 获取access_token填写client_credential
appid 是 第三方用户唯一凭证
secret 是 第三方用户唯一凭证密钥,既appsecret
返回说明
正确的Json返回结果:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
根据上面的办法 ,进入开发模式后,可以看到腾讯分配给你的APPID 和 APPSECRET放到上面URL中相应的位置,直接复制到浏览器地址栏,就看到返回的JSON,然后那个一大串字符就是你的ACCESS_TOKEN
呵呵,不废话话了,上代码,很简单的!
<?php
function post($url, $jsonData){
$ch = curl_init($url) ;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch) ;
curl_close($ch) ;
return $result;
}
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=你的ACCESS_TOKEN";
$data = "{
"button":[
{
"type":"click",
"name":"关于我们",
"sub_button":[
{
"type":"click",
"name":"医院介绍",
"key":"V1001_01_01"
},
{
"type":"click",
"name":"金牌服务",
"key":"V1001_01_02"
},
{
"type":"click",
"name":"最新活动",
"key":"V1001_01_03"
},
{
"type":"click",
"name":"医院地址",
"key":"V1001_01_04"
},
{
"type":"click",
"name":"每日话题",
"key":"V1001_01_05"
}]
},
{
"type":"click",
"name":"会员",
"sub_button":[
{
"type":"click",
"name":"会员信息",
"key":"V1001_02_01"
},
{
"type":"click",
"name":"星座圈",
"key":"V1001_02_02"
},
{
"type":"click",
"name":"微信墙",
"key":"V1001_02_03"
},
{
"type":"click",
"name":"积分兑换",
"key":"V1001_02_04"
}]
},
{
"type":"click",
"name":"帮助",
"sub_button":[
{
"type":"click",
"name":"使用帮助",
"key":"V1001_03_01"
},
{
"type":"click",
"name":"最新更新",
"key":"V1001_03_02"
}]
}]
}";
post($url,$data);
?>