码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2016-09-26 12:40:28      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

根据微信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 }

 

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

标签:

原文地址:http://www.cnblogs.com/dreamysky/p/5908636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!