网站网页显示微信授信登录二维码
1.去微信开放平台注册,获取 appid 和 appsecret , 并设置回调的在线域名 等
2.付300元进行开发者资质认证,微信才会给你接口权限
3.在登陆网站下建两个 PHP 文件
4.好了 开始写代码
一 . wx.php 调取微信提供的二维码(可嵌入登录页面),参数参考文档
"
二.wx_callback.php 接收微信返回的参数
<?php
header("Content-Type: text/html;charset=utf-8");
session_start();
$info=$_GET;
$code = $_GET['code'];
$state = $_GET['state'];
$rand=$_SESSION["wx_rand"]
//扫码后同意授权 code 有值,判断 rand 是否异常
if ($code=='' && $rand!==$state) {
exit();
}else{
$appid = ''; //自己的id和秘钥
$appsecret = '';
//通过code获取access_token
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token = json_decode(file_get_contents($token_url),true);
$r_token=$token['refresh_token'];
//刷新或续期access_token
$refresh_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$r_token;
$refresh_token = json_decode(file_get_contents($refresh_token_url),true);
$access_token=$refresh_token['access_token'];
$openid=$refresh_token['openid'];
//检验授权凭证(access_token)是否有效 有效期为 2小时
$verify_access_token_url='https://api.weixin.qq.com/sns/auth?access_token='.$access_token.'&openid='.$openid;
$res = json_decode(file_get_contents($verify_access_token_url),true);
//获取 unionid
if ($res['errcode']==0 && $res['errmsg']== ok) {
$user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$user_info = json_decode(file_get_contents( $user_info_url),true);
//var_dump($user_info);exit();
$unionid=$user_info['unionid'];//unionid为唯一,可区分用户,绑定unionid就可以进行登录了
{
}
?>
问题:登录成功自动跳转时,发现展示页面被 iframe框架套住
解决:
echo "
";
即可
$redirect_uri="网址";
$redirect_uri=urlencode($redirect_uri);//该回调需要url编码
$appID="**********";
$scope="snsapi_login";//写死,微信暂时只支持这个值
//准备向微信发请求
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $appID."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=STATE#wechat_redirect";
//请求返回的结果(实际上是个html的字符串)
$result = file_get_contents($url);
//替换图片的src才能显示二维码
$result = str_replace("/connect/qrcode/", "https://open.weixin.qq.com/connect/qrcode/", $result);
echo $result;
//return $result; //返回页面