php微信授权获取用户信息40003,微信返回40003错误 请求授权时要到openid 和 access_token...

//清除$access_token  从新获取正确的$access_token

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;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值