微信网页授权获取用户信息
Posted Objects
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信网页授权获取用户信息相关的知识,希望对你有一定的参考价值。
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 }
以上是关于微信网页授权获取用户信息的主要内容,如果未能解决你的问题,请参考以下文章