过程:点击页面上的微信登陆,打开一个弹出,弹出的内容是一个网页,网页的地址是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);
}