码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2016-07-30 18:22:22      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

 首先引导用户去授权页面:
地址:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=redirect_uri&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
用户同意授权后,页面将跳转至 redirect_uri/?code=CODE&state=STATE

技术分享
 1 <?php
 2 
 3 //通过code换取token  
 4 $code = $_GET[‘code‘];  
 5 $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=SECRET&code=$code&grant_type=authorization_code";  
 6 $json = file_get_contents($url);  
 7 $arr = json_decode($json,true);  
 8 $token = $arr[‘access_token‘];  
 9 $openid = $arr[‘openid‘];  
10 //拿到token后就可以获取用户基本信息了  
11 $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid ";  
12 $json = file_get_contents($url);//获取微信用户基本信息  
13 $arr = json_decode($json,true);  
14 $name = $arr[‘nickname‘];//昵称  
15 $imgURL = $arr[‘headimgurl‘];//头像地址  
16 $sex = $arr[‘sex‘];//性别  
17 $province = $arr[‘province‘];//用户个人资料填写的省份  
18 $city= $arr[‘city‘];//普通用户个人资料填写的城市  
19 $country= $arr[‘country‘];//国家,如中国为CN
20 
21 ?>
View Code
<?php

//通过code换取token  
$code = $_GET[‘code‘];  
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=SECRET&code=$code&grant_type=authorization_code";  
$json = file_get_contents($url);  
$arr = json_decode($json,true);  
$token = $arr[‘access_token‘];  
$openid = $arr[‘openid‘];  
//拿到token后就可以获取用户基本信息了  
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid ";  
$json = file_get_contents($url);//获取微信用户基本信息  
$arr = json_decode($json,true);  
$name = $arr[‘nickname‘];//昵称  
$imgURL = $arr[‘headimgurl‘];//头像地址  
$sex = $arr[‘sex‘];//性别  
$province = $arr[‘province‘];//用户个人资料填写的省份  
$city= $arr[‘city‘];//普通用户个人资料填写的城市  
$country= $arr[‘country‘];//国家,如中国为CN

?>

 

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

标签:

原文地址:http://www.cnblogs.com/guomengtao/p/5721282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!