Get wechat access_token

 

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时(7200s),需定时刷新,重复获取将导致上次获取的access_token失效。

公众平台的API调用所需的access_token的使用及生成方式说明:

1、为了保密appsecrect,第三方需要一个access_token获取和刷新的中控服务器。而其他业务逻辑服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则会造成access_token覆盖而影响业务;
2、目前access_token的有效期通过返回的expire_in来传达,目前是7200秒之内的值。中控服务器需要根据这个有效时间提前去刷新新access_token。在刷新过程中,中控服务器对外输出的依然是老access_token,此时公众平台后台会保证在刷新短时间内,新老access_token都可用,这保证了第三方业务的平滑过渡;
3、access_token的有效时间可能会在未来有调整,所以中控服务器不仅需要内部定时主动刷新,还需要提供被动刷新access_token的接口,这样便于业务服务器在API调用获知access_token已超时的情况下,可以触发access_token的刷新流程。

如果第三方不使用中控服务器,而是选择各个业务逻辑点各自去刷新access_token,那么就可能会产生冲突,导致服务不稳定。

公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在微信公众平台官网-开发者中心页中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。

接口调用请求说明

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}
参数说明
access_token获取到的凭证
expires_in凭证有效时间,单位:秒


错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):

{"errcode":40013,"errmsg":"invalid appid"}

 
 
 
1. PHP Curl
/**
* Get the "access_token" by php curl
* @return mixed return "access_token"(proof) and "expires_in"(valid time)
*/
function getAccessToken() {

$AppID = 'wx579e43a4729b1764';
$AppSecret = 'b90b1beaaef5e8e0e1f51647a3ae5cbc';
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$AppID&secret=$AppSecret";

$ch = curl_init($token_url);
// curl_setopt($ch, CURLOPT_TIMEOUT, 500);
// 跳过证书检查,同时 CURL_SSL_VERIFYHOST 需要被设置为 false。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$token_data = curl_exec($ch);

if(curl_errno($ch))
echo "ERROR: ".curl_error($ch);

curl_close($ch);

// note: normally, wechat will return the JSON data
// {"access_token":"ACCESS_TOKEN","expires_in":7200}
// json_decode() will decode a JSON string into a PHP variable
$token_data = json_decode($token_data);

return $token_data->access_token;

}
 
 
2、file_get_contents()
 
/**
* Get the "access_token" by file_get_contents()
* @return mixed return "access_token"(proof) and "expires_in"(valid time)
*/
function getAccessToken() {

$AppID = 'wx579e43a4729b1764';
$AppSecret = 'b90b1beaaef5e8e0e1f51647a3ae5cbc';
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$AppID&secret=$AppSecret";

$timeout = stream_context_create(array(
'http' => array(
'timeout' => 10,
),
));
$token_data = file_get_contents($token_url, 0, $timeout);

// note: normally, wechat will return the JSON data
// {"access_token":"ACCESS_TOKEN","expires_in":7200}
// json_decode() will decode a JSON string into a PHP variable
$token_data = json_decode($token_data);

return $token_data->access_token;

}

转载于:https://www.cnblogs.com/lizsCnBlog/p/4923642.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值