if($classData->errcode == '42001' || $classData->errcode == '40001'){
$this->_clearAccessToken($this->token);
$access_token = $this->_getAccessToken ();
LOG::write( "PassportAction.class.php, re openid:$openid");
$url2 = 'https://api.weixin.qq.com/cgi-bin/user/info?openid=' . $openid . '&access_token=' . $access_token;
$classData = json_decode ( curlGet ( $url2 ) );
LOG::write( "PassportAction.class.php, re addUserInfo(..)");
}
有两种情况 主要可能就是access_token 太旧了不能获取 或者不对 要从新获取
/**
* 清空accesstoken ywfnew 2015-4-21
*/
private function _clearAccessToken($token){
$where = array (
'token' => $token
);
$this->thisWxUser['access_token'] = '';
$this->thisWxUser['acsgettime'] = 0;
$this->thisWxUser['acsexpires_in'] = 0;
$mt = M ( 'Wxuser' )->where ( $where )->save ($this->thisWxUser);
if($mt){
LOG::write("PassportAction.class.php:_clearAccessToken(..), update suc!");
}else{
LOG::write("PassportAction.class.php:_clearAccessToken(..), update faild!");
}
}
//重新获取
private function _getAccessToken() {
$where = array (
'token' => $this->token
);
$this->thisWxUser = M ( 'Wxuser' )->where ( $where )->find ();
// token 有效,没有超时。
if ($this->thisWxUser ['access_token'] && ($this->thisWxUser ['acsgettime'] + $this->thisWxUser ['acsexpires_in']) > time ()) {
$access_token = $this->thisWxUser ['access_token'];
} else {
$globalurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->thisWxUser ['appid'] . "&secret=" . $this->thisWxUser ['appsecret'];
$globalaccesObj = json_decode ( curlGet ( $globalurl ) );
if (! $globalaccesObj->errmsg) {
// 更新token
$this->thisWxUser ['access_token'] = $globalaccesObj->access_token;
$access_token = $globalaccesObj->access_token;
$this->thisWxUser ['acsgettime'] = time ();
$this->thisWxUser ['acsexpires_in'] = $globalaccesObj->expires_in;
$wxmodel = M ( 'wxuser' )->save ( $this->thisWxUser );
} else {
LOG::write ( '获取globalaccesObj发生错误:错误代码' . $globalaccesObj->errcode . ',微信返回错误信息:' . $globalaccesObj->errmsg );
exit ();
}
}
return $access_token;
}
特殊说明,本文版权归 ning个人博客 所有带原创标签请勿转载,转载请注明出处.