您的位置:首页 > 移动开发 > 微信开发

微信接口开发之高级篇系列【网页授权详细说明【提供测试账号使用】】

2016-09-09 22:34 701 查看
本篇文章你将学到:在自己做的微信网站里,利用oauth2.0网页授权接口获取用户的信息(openid,姓名,性别,地区,头像等)。如大转盘等游戏记录哪个微信用户获得什么奖品、H5等小游戏需要把分数与对应用户捆绑在一起等网页应用。

微信公众平台oauth2.0网页授权能干什么

它是在自己做的网站中不用用户登录来获取微信用户相关信息的,进而实现相关业务。

说明与注意

1、网页授权分为两种,

一种为只获取openid (基本授权 snsapi_base)

一种为获取用户全部信息 (高级授权 snsapi_userinfo)。

2、你的公众号必须为认证的订阅号或者认证的服务号。否则没有此接口权限。

3、你要配置好回调域名:即用户点击网址获取用户信息后打开哪个域名。

4、如有下图错误请检查是否配置好回调域名或者公众号是否认证(我之前一直测试提示如下图出错,仔细查找错误才发现没配置回调域名)



怎样配置回调域名

1、进入https://mp.weixin.qq.com,点击最下面的”接口权限“菜单(如下图)



1-1、如果是测试账号的话,如下图

  (1)打开浏览器,这里以IE为例,输入:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

(2)






(3)用手机登录你的微信,使用微信中的“扫一扫”功能,扫描上面网页中的二维码。在手机上会出现以下界面:







(4)网页授权获取用户基本信息

2、找到‘网页授权用户基本信息’,如下图



3、点击修改,填写域名。如:我的回调网址为http://wechatu.xd107.com/home/WeiXin/index 则填写wechatu.xd107.com。配置回调域名完成。

不管获取openid还是用户所有信息都需要首先配置回调域名



4、实际代码(ThinkPhp框架)

class WeiXinController extends Controller
{

/**
* 这里采用高级授权模式,为了获取用户信息
* 这个页面是用户进来就能够刚问的页面,也就是首次进来的页面
* 首先访问的地址 ;http://wechatu.xd107.com/home/WeiXin/index
*/
public function index()
{
$appid = 'wx94c43716d8a91f3f';
/*基本授权 方法跳转地址*/
$redirect_uri = urlencode('http://wechatu.xd107.com/home/WeiXin/getUserInfo');

/*基本授权 snsapi_base*/
//$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1234#wechat_redirect";

/*高级授权 snsapi_userinfo*/
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect";
//跳转
header('location:' . $url);
}

/*拉取用户信息*/
public function getUserInfo()
{
$appid = 'wx94c43716d8a91f3f';
$appsecret = 'd4624c36b6795d1d99dcf0547af5443d';
/*回调的时候自带的这个参数*/
$code = $_GET['code'];

/*基本授权 snsapi_base*/
//$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1234#wechat_redirect";
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code";

$result = json_decode(curlPost($url,$parm = null),true);

/*取出数组中的access_token这个值*/
$access_token = $result['access_token'];
$openid = $result['openid'];
$URL2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$responseInfo = json_decode(curlPost($URL2,$parameter = null),true);
$_SESSION['headimgurl'] = $responseInfo['headimgurl'];
var_dump($responseInfo);
die;
$this->headimgurl = $responseInfo['headimgurl'];
$this->userInfo = $responseInfo;
$this->display();
}
}


5、流程图(百度脑图)

说明:开始详解:(1)网页授权分为两种,(2)微信公众账号和用户的微信联系字段为【openid】



6、具体步骤:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐