365体育备用网址报道7.3.1网页授权猎取个人信息_365体育备用网址官网资讯

以下PHP SDK代码实现了猎取用户信息的功能。

 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 }

当使用网页授权时,使用下面代码猎取code,然后进行跳转获得access Token信息,进而获得用户基本信息。

 1 require_once('weixin.class.php');
 2 $weixin = new class_weixin();
 3 
 4 if (!isset($_GET["code"])){
 5     $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
 6     $jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_userinfo", "123");
 7     Header("Location: $jumpurl");
 8 }else{
 9     $access_token_oauth2 = $weixin->oauth2_access_token($_GET["code"]);
10     $userinfo = $weixin->oauth2_get_user_info($access_token_oauth2['access_token'], $access_token_oauth2['openid']); 
11 }
Web页面代码如下所示。
1 <!DOCTYPE HTML>
 2 <html lang="zh-cn">
 3     <head>
 4         <meta charset="UTF-8">
 5         <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
 6         <title>网页授权Demo</title>
 7         <link rel="stylesheet" href="CSS/weui.min.css">
 8         <link rel="stylesheet" href="css/example.css">
 9     </head>
10     <body ontouchstart="">
11         <div class="container js_container">
12             <div class="page cell">
13                 <div class="hd">
14                     <h1 class="page_title">微信网页授权</h1>
15                     <p class="page_desc">方倍工作室 出品</p>
16                 </div>
17                 <div class="bd">
18                     <div class="weui_cells_title">个人信息</div>
19                     <div class="weui_cells">
20                         <div class="weui_cell">
21                             <div class="weui_cell_bd weui_cell_primary">
22                                 <p>OpenID</p>
23                             </div>
24                             <div class="weui_cell_ft"><?php echo $userinfo["openid"];?></div>
25                         </div>
26                         <div class="weui_cell ">
27                             <div class="weui_cell_bd weui_cell_primary">
28                                 <p>头像</p>
29                             </div>
30                             <div class="weui_cell_ft"><img src="<?php echo str_replace("/0","/46",$userinfo["headimgurl"]);?>"></div>
31                         </div>
32                         <div class="weui_cell">
33                             <div class="weui_cell_bd weui_cell_primary">
34                                 <p>昵称</p>
35                             </div>
36                             <div class="weui_cell_ft"><?php echo $userinfo["nickname"];?></div>
37                         </div>
38                         <div class="weui_cell">
39                             <div class="weui_cell_bd weui_cell_primary">
40                                 <p>性别</p>
41                             </div>
42                             <div class="weui_cell_ft"><?php echo (($userinfo["sex"] == 0)?"未知":(($userinfo["sex"] == 1)?"男":"女"));?></div>
43                         </div>
44                         <div class="weui_cell">
45                             <div class="weui_cell_bd weui_cell_primary">
46                                 <p>地区</p>
47                             </div>
48                             <div class="weui_cell_ft"><?php echo $userinfo["country"];?> <?php echo $userinfo["province"];?> <?php echo $userinfo["city"];?></div>
49                         </div>
50                         <div class="weui_cell">
51                             <div class="weui_cell_bd weui_cell_primary">
52                                 <p>语言</p>
53                             </div>
54                             <div class="weui_cell_ft"><?php echo $userinfo["language"];?></div>
55                         </div>
56                     </div>
57                 </div>
58             </div>
59         </div>
60     </body>
61 </html>

图7-23 OAuth2.0猎取个人信息

------分隔线----------------------------

公布评论 (157人察看0条评论)
请自觉遵守互联网相关的政策法规,严禁公布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换明升88注册送79
最新评论