7.3.1 网页授权获取个人信息
以下PHP代码实现了获取用户信息的功能。
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.0的URL16 public function oauth2_authorize($redirect_url, $scope, $state = NULL)17 {18 $url = "https:// open.weixin.qq.com/connect/oauth2/authorize?appid=".$th is->appid."&redirect_uri=".$redirect_url."&response_type=code&scope=". $scope."&state=".$state."#wechat_redirect";19 return $url;20 }21 22 // 生成OAuth2.0的Access Token23 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=authori zation_code";26 $res = $this->http_request($url);27 return json_decode($res, true);28 }29 30 // 获取用户基本信息(OAuth2.0授权的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 <p>12 <p>13 <p>14 <h1>微信网页授权</h1>15 <p>方倍工作室 出品</p>16 </p>17 <p>18 <p>个人信息</p>19 <p>20 <p>21 <p>22 <p>OpenID</p>23 </p>24 <p><?php echo $userinfo["op enid"];?></p>25 </p>26 <p>27 <p>28 <p>头像</p>29 </p>30 <p><img src="<?php echo str _replace("/0","/46",$userinfo["headimgurl"]);?>"> </p>31 </p>32 <p>33 <p>34 <p>昵称</p>35 </p>36 <p><?php echo $userinfo["nick name"];?></p>37 </p>38 <p>39 <p>40 <p>性别</p>41 </p>42 <p><?php echo (($userinfo["sex"] == 0)?"未知":(($userinfo["sex"] == 1)?"男":"女"));?> </p>43 </p>44 <p>45 <p>46 <p>地区</p>47 </p>48 <p><?php echo $userinfo["coun try"];?> <?php echo $userinfo["province"];?> <?php e cho $userinfo["city"];?></p>49 </p>50 <p>51 <p>52 <p>语言</p>53 </p>54 <p><?php echo $userinfo["lan guage"];?></p>55 </p>56 </p>57 </p>58 </p>59 </p>60 </body>61 </html>
OAuth2.0获取个人信息如图7-23所示。