- 网页授权接口(类似于浏览器通过cookie|session标识客户 微信端通过openid标识)
接口类型(通过网页授权 获取openid)
- snsapi_base:基本类型
- 静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面) 只获取openid
- snsapi_userinfo:详细类型
- 弹出窗口是否授权 需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息 (openid,昵称,头像,国籍,地理位置...)
- 用户管理类接口中的“获取用户基本信息接口”,是在用户和公众号产生消息交互或关注后事件推送后,才能根据用户OpenID来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。
接口步骤
- 用户同意授权 获取code
- 通过code换取网页授权access_token
- 刷新access_token(如果需要 避免过期)
- 拉取用户信息(需scope为snsapi_userinfo) 通过网页授权access_token和openid获取用户基本信息(支持UnionID机制 UnionID机制的作用说明:如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为同一用户,对同一个微信开放平台下的不同应用(移动应用、网站应用和公众帐号),unionid是相同的。)
关于网页授权access_token和普通access_token的区别(类似于雷锋和雷峰塔)
- 微信网页授权是通过OAuth2.0机制实现的,在用户授权给公众号后,公众号可以获取到一个网页授权特有的接口调用凭证(网页授权access_token),通过网页授权access_token可以进行授权后接口调用,如获取用户基本信息;
- 其他微信接口,需要通过基础支持中的“获取access_token”接口来获取到的普通access_token调用
基本授权(获取openid)
- 调用接口获取code
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect" ;
- 组装参数 appid|appsecret|code调用接口 获取openid
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code" ;
详细授权
- 调用接口获取code
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect" ;
- 组装参数 appid|appsecret|code调用接口 获取网页授权access_token openid
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code" ;
- 组装参数 网页授权access_token openid 调用接口 拉取用户详细信息
$url1 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN" ;
/*
网页授权
*/
//获取用户openid
public function getBaseInfo()
{
//获取code
$appid = "XXXXXXX" ;
/*
调用下面url之后 返回code到 此处的$redirect_uri
接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名 donglingjiu.top
*/
$redirect_uri = urlencode("http://donglingjiu.top/index/index/getUserOpenId");
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect" ;
/*
scope=snsapi_base 静默跳转
*/
header('location:'.$url) ;
}
public function getUserOpenId()
{
//获取网页授权access_token
$appid = "XXXXXXX" ;
$appsecret = "XXXXXXXXX" ;
/*
scope=snsapi_base
静默跳转之后 传过来的code
*/
$code = $_GET['code'] ;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code" ;
//拉取用户openid
$res = $this->http_curl($url,'get') ;
dump($res) ;
/*
此处测试
百度 草料二维码
http://donglingjiu.top/index/index/getBaseInfo
生成二维码之后 手机扫码
*/
}
/*
详细授权 获取用户 昵称 地理位置 籍贯 头像...
*/
public function getUserDetail()
{
//获取code
$appid = "XXXXXXXXX" ;
/*
调用下面url之后 返回code到 此处的$redirect_uri
接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名 donglingjiu.top
*/
$redirect_uri = urlencode("http://donglingjiu.top/index/index/getUserInfo");
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect" ;
/*
scope=snsapi_userinfo 手动授权 获取详细信息
*/
header('location:'.$url) ;
}
public function getUserInfo()
{
//获取网页授权access_token
$appid = "XXXXXXXXX" ;
$appsecret = "XXXXXXXXX" ;
/*
scope=snsapi_userinfo
手动授权 跳转之后 传过来的code
*/
$code = $_GET['code'] ;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code" ;
$res = $this->http_curl($url,'get') ;
$access_token = $res['access_token'] ;
$openid = $res['openid'] ;
//拉取用户详细信息
$url1 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN" ;
$res = $this->http_curl($url1) ;
dump($res) ;
/*
此处测试
百度 草料二维码
http://donglingjiu.top/index/index/getUserDetail
生成二维码之后 手机扫码
*/
}