code been used, hints: [ req_id: XYv1Ha07042046 ]

怼烎@ 2023-01-16 11:08 87阅读 0赞
  1. public function index()
  2. {
  3. $code = input('get.code');
  4. $tool = new Wxtool();
  5. if (empty($code))
  6. {
  7. $url = 'http://www.layton.top/wx/Receive';
  8. $tool -> getCode($url, 2);
  9. }else
  10. {
  11. $tool -> getWebAccessToken($code);
  12. }
  13. }
  14. public function getCode($url,$type=1)
  15. {
  16. $redirect_uri = urlencode($url);
  17. $scope = null;
  18. if($type == 1)
  19. {
  20. $scope = 'snsapi_base';
  21. }else{
  22. $scope = 'snsapi_userinfo';
  23. }
  24. $code = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . APPID .'&redirect_uri=' . $redirect_uri . '&response_type=code&scope=' . $scope . '&state=STATE#wechat_redirect';
  25. header("location:" . $code);
  26. }
  27. public function getWebAccessToken($code)
  28. {
  29. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . APPID . '&secret=' . APPSECRET . '&code=' . $code . '&grant_type=authorization_code';
  30. $data = $this -> http_curl($url, 'get');
  31. dump($data);
  32. }

我们第一次进入的时候是可以正常获取返回数据的,但是刷新一下就不行了,这个是为什么呢?

没错,他第二次用的code参数是第一次获取的,微信的code只能用一次:

  1. code说明:code作为换取access_token的票据,每次用户授权携带的code将不一样,code只能使用一次,5分钟未被使用将自动过期.

所以第二次用的时候就不行了。我们正常的使用流程应该这样:
在这里插入图片描述
优秀PHP项目推荐:http://github.crmeb.net/u/defu

发表评论

表情:
评论列表 (有 0 条评论,87人围观)

还没有评论,来说两句吧...

相关阅读