微信网页授权获取用户信息

 1 class class_weixin
 2 {
 3     var $appid = APPID;
 4     var $appsecret = APPSECRET;
 5 
 6     //构造函数,获取Access Token
 7     public function __construct($appid = NULL, $appsecret = NULL)
 8     {
 9         if($appid && $appsecret){
10             $this->appid = $appid;
11             $this->appsecret = $appsecret;
12         }
13     }
14 
15     //生成OAuth2的URL
16     public function oauth2_authorize($redirect_url, $scope, $state = NULL)
17     {
18         $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$redirect_url."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";
19         return $url;
20     }
21 
22     //生成OAuth2的Access Token
23     public function oauth2_access_token($code)
24     {
25         $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
26         $res = $this->http_request($url);
27         return json_decode($res, true);
28     }
29 
30     //获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取)
31     public function oauth2_get_user_info($access_token, $openid)
32     {
33         $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
34         $res = $this->http_request($url);
35         return json_decode($res, true);
36     }
37 
38     //获取用户基本信息
39     public function get_user_info($openid)
40     {
41         $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
42         $res = $this->http_request($url);
43         return json_decode($res, true);
44     }
45 
46     //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
47     protected function http_request($url, $data = null)
48     {
49         $curl = curl_init();
50         curl_setopt($curl, CURLOPT_URL, $url);
51         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
52         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
53         if (!empty($data)){
54             curl_setopt($curl, CURLOPT_POST, 1);
55             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
56         }
57         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
58         $output = curl_exec($curl);
59         curl_close($curl);
60         return $output;
61     }
62 }

 

转载于:https://www.cnblogs.com/objects/p/7146620.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值