微信授权登陆获取用户信息

根据微信api和自己实际项目开发;

 1  public function getuserinfo()
 2     {
 3         if (isset($_GET['code'])) {
 4             //获取access_token  openid  refresh_token
 5             $url1 = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . $_GET['code'] . '&grant_type=authorization_code';
 6             $result_access_token = $this->http_curl($url1); //主要返回取access_token 和 openid
 7             $arr = json_decode($result_access_token, true); //数组
 8             //验证access_token是否有效
 9             $url_fresh_verification = 'https://api.weixin.qq.com/sns/auth?access_token=' . $arr['access_token'] . '&openid=' . $arr['openid'] . '';
10             $refresh_token_istrue = $this->http_curl($url_fresh_verification);
11             $refresh_token_istrue = json_decode($refresh_token_istrue, true);
12             if ($refresh_token_istrue['errcode'] != 0) {
13                 //3.0 重新刷新access_token
14                 $url_get_new_access_token = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wxeb8d4ef8686310eb&grant_type=refresh_token&refresh_token=' . $arr['refresh_token'] . '';
15                 $new_access_token = $this->http_curl($url_get_new_access_token);
16                 $obj = json_decode($new_access_token);
17                 //重新取得access_token
18                 $arr['access_token'] = $obj->access_token;
19             }
20             // 获取用户信息
21             if (count($arr) > 2) {
22                 $url_userinfo = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $arr['access_token'] . '&openid=' . $arr['openid'] . '&lang=zh_CN';
23                 $result_userinfo = $this->http_curl($url_userinfo);
24                 //dump(json_decode($result_userinfo,true));
25             } else {
26                 echo json_encode($arr);
27             }
28         } else {
29             echo "授权失败";
30         }
31     }

curl会话机制执行url

1 public function http_curl($url){
2     $ch=curl_init();//开启会话机制
3     curl_setopt($ch,CURLOPT_URL,$url);
4     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //获取数据返回
5     curl_setopt($ch,CURLOPT_BINARYTRANSFER,true); //启用
6     $result=curl_exec($ch); //执行
7     curl_close($ch); //关闭会话机制
8     return json_decode($result,true); //true 返回数组,默认返回对象
9 }

 

转载于:https://www.cnblogs.com/dreamysky/p/5908636.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值