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


     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 }
  • 相关阅读:
    SSL JudgeOnline 1194——最佳乘车
    SSL JudgeOnline 1457——翻币问题
    SSL JudgeOnlie 2324——细胞问题
    SSL JudgeOnline 1456——骑士旅行
    SSL JudgeOnline 1455——电子老鼠闯迷宫
    SSL JudgeOnline 2253——新型计算器
    SSL JudgeOnline 1198——求逆序对数
    SSL JudgeOnline 1099——USACO 1.4 母亲的牛奶
    SSL JudgeOnline 1668——小车载人问题
    SSL JudgeOnline 1089——USACO 1.2 方块转换
  • 原文地址:https://www.cnblogs.com/objects/p/7146620.html
Copyright © 2020-2023  润新知