防止Access_token过期的方法
2016-11-25 15:26
351 查看
<SPAN style="FONT-SIZE: 14px"> //得到订阅用户 (返回数组)
public function GetUserList()
{
$strjson = $this -> GetUrlReturn("https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s");
$openidarr= $strjson->data->openid;
//print_r($openidarr); 调试
return $openidarr;
}
//得到订阅用户详情(返回对象)
public function GetUserDetail($openid)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid={$openid}";
$strjson = $this -> GetUrlReturn($url);
return $strjson;
}
/*
*
* 私有成员变量 存token值
* 因为//access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。
* 正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。
*/
private $_token ;
/*
*
* 私有方法
*
*/
//得到Token对象并写入到配置文件
private function InitToken()
{
$url = sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",APPID, SECRET);
//echo APPID;
$ch = curl_init(); //创建一个新url资源
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);
$strjson=json_decode($a);
$token = $strjson->access_token;
if (empty($token))
{
//修改 {"errcode":45009,"errmsg":"api freq out of limit"}
echo "错误:取得token无效,可能是调用太频繁!"; //$strjson
throw new Exception('错误:取得token无效');
}
$obj = fopen("saestor://weixindata/token.txt","w+"); //SAE禁用fopen本地文件,这里需要Storage
fwrite($obj,$token);
$this -> _token = $token;
}
//封装私有方法,调用得到Get的参数,$needToken默认为false, 不取值,这里有一个潜规则,%s为 self::$_token
private function GetUrlReturn($url, $needToken = false)
{
//第一次为空,则从文件中读取
if (empty($this -> _token))
{
$obj = fopen("saestor://weixindata/token.txt","r");
$this -> _token = fgets($obj,1000);
}
//为空则重新取值
if (empty($this -> _token) || $needToken)
{
$this ->InitToken();
}
$newurl = sprintf($url, $this -> _token);
$ch = curl_init(); //创建一个新url资源
curl_setopt($ch, CURLOPT_URL,$newurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);
$strjson=json_decode($a);
//var_dump($strjson); //开启可调试
if (!empty($strjson-> errcode))
{
switch ($strjson-> errcode){
case 40001:
$this -> GetUrlReturn($url, true); //重新取值,可能是过期导致
break;
case 41001:
throw new Exception("缺少access_token参数:".$strjson->errmsg);
break;
default:
throw new Exception($strjson->errmsg); //其他错误,抛出
break;
}
}
return $strjson;
}</SPAN>
public function GetUserList()
{
$strjson = $this -> GetUrlReturn("https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s");
$openidarr= $strjson->data->openid;
//print_r($openidarr); 调试
return $openidarr;
}
//得到订阅用户详情(返回对象)
public function GetUserDetail($openid)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid={$openid}";
$strjson = $this -> GetUrlReturn($url);
return $strjson;
}
/*
*
* 私有成员变量 存token值
* 因为//access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。
* 正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。
*/
private $_token ;
/*
*
* 私有方法
*
*/
//得到Token对象并写入到配置文件
private function InitToken()
{
$url = sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",APPID, SECRET);
//echo APPID;
$ch = curl_init(); //创建一个新url资源
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);
$strjson=json_decode($a);
$token = $strjson->access_token;
if (empty($token))
{
//修改 {"errcode":45009,"errmsg":"api freq out of limit"}
echo "错误:取得token无效,可能是调用太频繁!"; //$strjson
throw new Exception('错误:取得token无效');
}
$obj = fopen("saestor://weixindata/token.txt","w+"); //SAE禁用fopen本地文件,这里需要Storage
fwrite($obj,$token);
$this -> _token = $token;
}
//封装私有方法,调用得到Get的参数,$needToken默认为false, 不取值,这里有一个潜规则,%s为 self::$_token
private function GetUrlReturn($url, $needToken = false)
{
//第一次为空,则从文件中读取
if (empty($this -> _token))
{
$obj = fopen("saestor://weixindata/token.txt","r");
$this -> _token = fgets($obj,1000);
}
//为空则重新取值
if (empty($this -> _token) || $needToken)
{
$this ->InitToken();
}
$newurl = sprintf($url, $this -> _token);
$ch = curl_init(); //创建一个新url资源
curl_setopt($ch, CURLOPT_URL,$newurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);
$strjson=json_decode($a);
//var_dump($strjson); //开启可调试
if (!empty($strjson-> errcode))
{
switch ($strjson-> errcode){
case 40001:
$this -> GetUrlReturn($url, true); //重新取值,可能是过期导致
break;
case 41001:
throw new Exception("缺少access_token参数:".$strjson->errmsg);
break;
default:
throw new Exception($strjson->errmsg); //其他错误,抛出
break;
}
}
return $strjson;
}</SPAN>
相关文章推荐
- 微信调用接口,防止Access_token过期的方法
- 微信开发-access_token 过期解决方法
- 防止重复提交表单---Struts中Token的使用方法
- 百度PCS快速获取access_token的方法
- C# 微信access_token缓存和过期刷新
- PHP使用token防止表单重复提交的方法
- iOS 微博 sdk access_token 过期重登
- 防止session值过期的方法
- .Net微信开发之如何解决access_token过期问题
- 【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永不过期
- 微信开发,access_token,时间上没有过期,但已失效的问题
- 百度PCS快速获取access_token的方法
- 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题
- (45)使用RandomAccessFile向中间部分插入内容防止覆盖后面的内容方法
- 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题 .
- 微信公共平台开发-(.net实现)5--access_token过期的问题
- 淘宝开放平台 TOP AccessToken SessionKey 过期问题烦恼 找远哥帮忙吧
- 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题
- 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题(转)
- 微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题