Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信页面授权登陆 #38

Open
lovecn opened this issue Nov 20, 2016 · 0 comments
Open

微信页面授权登陆 #38

lovecn opened this issue Nov 20, 2016 · 0 comments

Comments

@lovecn
Copy link
Owner

lovecn commented Nov 20, 2016

from http://everyding.github.io/2016/04/08/weixin-auth/
首先登陆微信公众平台,点击左侧菜单 开发 -> 接口权限,找到‘网页授权获取用户基本信息 点击修改,然后填上需要授权的域名
1.获取code

只需要访问:https://open.weixin.qq.com/connect/oauth2/authorize?appid=您的appid&redirect_uri=授权地址成功后跳转URL&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect

2.通过code获取access_token,再根据access_token获取userinfo

//通过code获取access_token
$code = $_GET['code'];
$appid = "您的appid";
$appsecret = "您的appsecret";
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
$result = https_request($url);
$jsoninfo = json_decode($result, true);

//根据access_token获取userinfo
$openid = $jsoninfo["openid"];
$access_token = $jsoninfo["access_token"];
$url1 = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$result1 = https_request($url1);
$jsoninfo1 = json_decode($result1, true);

function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

//结果返回userinfo
echo json_encode($jsoninfo1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant