微信扫描登陆

过程:点击页面上的微信登陆,打开一个弹出,弹出的内容是一个网页,网页的地址是public function wxlogin()这个控制器的地址,用户扫码之后会回调到 wxlogincallback()方法,获取用户信息

///测试微信登录//
public function wxlogin(){
//-------配置
$AppID = 'wxeaa7115dff4fc477';
$AppSecret = '3ece6e0b82662d31018ec959300c3d37';
$callback = 'http://zyzl.wolfmedia.cn/index/Index/wxlogincallback'; //回调地址
//$callback = 'http://toutiao.weidong100.com/index/Index/wxlogincallback'; //回调地址
//微信登录
$state = md5(uniqid(rand(), TRUE));
$_SESSION["wx_state"] = $state;
$callback = urlencode($callback);
$wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=".$AppID."&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
header("Location: $wxurl");
}

//回调
public function wxlogincallback(){
if($_GET['state']!=$_SESSION["wx_state"]){
exit("5001");
}
$AppID = 'wxeaa7115dff4fc477';
$AppSecret = '3ece6e0b82662d31018ec959300c3d37';
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$AppID.'&secret='.$AppSecret.'&code='.$_GET['code'].'&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);
curl_close($ch);
$arr=json_decode($json,1);
//得到 access_token 与 openid
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$arr['access_token'].'&openid='.$arr['openid'].'&lang=zh_CN';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);
curl_close($ch);
$arr=json_decode($json,1);
//得到 用户资料
print_r($arr);

}

转载于:https://www.cnblogs.com/tingfengqieyu/p/5833745.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值