|
@@ -1,89 +0,0 @@
|
|
|
-<?php
|
|
|
-/**
|
|
|
- * Created by PhpStorm.
|
|
|
- * User: james
|
|
|
- * Date: 2017/4/25
|
|
|
- * Time: 下午3:43
|
|
|
- */
|
|
|
-
|
|
|
-include 'config.php';
|
|
|
-//判断state是否正确
|
|
|
-if(empty($_GET['state'])) {
|
|
|
- exit('参数错误,请重新登录');
|
|
|
-}
|
|
|
-
|
|
|
-if($_GET['state'] != $_SESSION['state']) {
|
|
|
- exit('<script>alert("参数错误!");
|
|
|
- location.href="../login.php";</script>');
|
|
|
-}
|
|
|
-
|
|
|
-//检查用户是否同意授权
|
|
|
-if(empty($_GET['code'])) {
|
|
|
- exit('<script>alert("您没有同意授权!");
|
|
|
- location.href="../login.php";</script>');
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
- * 通过code换取网页授权access_token
|
|
|
- * https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
|
|
|
- */
|
|
|
-$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.appID.
|
|
|
- '&secret='.appsecret.
|
|
|
- '&code='.$_GET['code'].
|
|
|
- '&grant_type=authorization_code';
|
|
|
-//通过curl获取access_token
|
|
|
-$res = myCurl($url);
|
|
|
-/*
|
|
|
- * 正确返回
|
|
|
- * { "access_token":"ACCESS_TOKEN","expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID","scope":"SCOPE" }
|
|
|
- * 错误返回
|
|
|
- * {"errcode":40029,"errmsg":"invalid code"}
|
|
|
- */
|
|
|
-
|
|
|
-//将json信息转换为数组
|
|
|
-$info = json_decode($res, true);
|
|
|
-//判断是否成功
|
|
|
-if(empty($info['access_token'])){
|
|
|
- exit('参数错误,请重新登录...');
|
|
|
-}
|
|
|
-
|
|
|
-/* 获取用户信息
|
|
|
- * 请求地址:
|
|
|
- * https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
|
|
|
- * 正确时返回的JSON数据包如下:
|
|
|
- * openid 用户的唯一标识
|
|
|
- * nickname 用户昵称
|
|
|
- * 其他相信查看手册
|
|
|
- * 错误时微信会返回JSON数据包如下(示例为openid无效):
|
|
|
- * {"errcode":40003,"errmsg":" invalid openid "}
|
|
|
-*/
|
|
|
-$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$info['access_token'].'&openid='.$info['openid'].'&lang=zh_CN';
|
|
|
-$res = myCurl($url);
|
|
|
-//将json信息转换为数组
|
|
|
-$user = json_decode($res, true);
|
|
|
-if(!empty($user['errcode'])) {
|
|
|
- exit('参数错误,请重新登录...');
|
|
|
-}
|
|
|
-
|
|
|
-//注册session 并返回登录页面
|
|
|
-$_SESSION['wxopenid'] = $user['openid'];
|
|
|
-$_SESSION['uface'] = $user['headimgurl'];
|
|
|
-$_SESSION['uname'] = $user['nickname'];
|
|
|
-
|
|
|
-header('location:');
|
|
|
-
|
|
|
-function myCurl($url, $type = 'GET', $data = null){
|
|
|
- $curl = curl_init();
|
|
|
- curl_setopt($curl, CURLOPT_URL , $url);
|
|
|
- curl_setopt($curl, CURLOPT_RETURNTRANSFER , true);
|
|
|
- if($type != 'GET'){
|
|
|
- curl_setopt($curl, CURLOPT_POST, 1);
|
|
|
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
|
|
- }
|
|
|
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);
|
|
|
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST , false);
|
|
|
- curl_setopt($curl, CURLOPT_ENCODING , 'gzip,deflate');
|
|
|
- $res = curl_exec($curl);
|
|
|
- curl_close($curl);
|
|
|
- return $res;
|
|
|
-}
|