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

php 微信第三方登录验证

2016-06-28 17:58 1051 查看
设置微信第三方登录的前提条件是

微信开发平台为:服务号

用户同意授权,获取code

在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo)

注意:如果提示scope参数错误 ,说明不是服务号

参数说明:

参数是否必须说明
appid公众号的唯一标识
redirect_uri授权后重定向的回调链接地址,请使用urlencode对链接进行处理
response_type返回类型,请填写code
scope应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息
state重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
#wechat_redirect无论直接打开还是做页面302重定向时候,必须带此参数
appid应用编号 |
redirect_uri:回调地址 |response_type | scope |
state

url:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

用户同意授权后

设置回调地址 开发->接口权限->网页授权获取用户基本信息



获取code信息

获取用户信息

通过code换取网页授权access_token

https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=app_secret&code=CODE&grant_type=authorization_code

可以获得access_token  也可获得openid

拉取用户信息(需scope为 snsapi_userinfo)

https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN


php可执行action

public function actionWx()
{
$url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->wx_app_id.'&redirect_uri='.$this->wx_redirect.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
header("Location:" . $url);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息