|
|
|
@ -18,40 +18,40 @@ if (!isset($_GET['code'])) { |
|
|
|
} |
|
|
|
|
|
|
|
// 获取 access_token 和 openid |
|
|
|
// $code = $_GET['code']; |
|
|
|
// if ($code != null){ |
|
|
|
// var_dump($code); |
|
|
|
// } else { |
|
|
|
// var_dump("code is null"); |
|
|
|
// } // code已经成功获取到 |
|
|
|
$code = $_GET['code']; |
|
|
|
if ($code != null){ |
|
|
|
// var_dump($code); |
|
|
|
} else { |
|
|
|
var_dump("code is null"); |
|
|
|
} // code已经成功获取到 |
|
|
|
|
|
|
|
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code'; |
|
|
|
$response = file_get_contents($url); |
|
|
|
echo $response; |
|
|
|
// $data = json_decode($response, true); |
|
|
|
// echo $data; |
|
|
|
// if (!$data || !isset($data['access_token']) || !isset($data['openid'])) { |
|
|
|
// // 授权失败 |
|
|
|
// echo '授权失败,请重试'; |
|
|
|
// exit; |
|
|
|
// } |
|
|
|
|
|
|
|
// // 获取用户信息 |
|
|
|
// $access_token = $data['access_token']; |
|
|
|
// $openid = $data['openid']; |
|
|
|
// $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN'; |
|
|
|
// $response = file_get_contents($url); |
|
|
|
// $userinfo = json_decode($response, true); |
|
|
|
// if (!$userinfo || !isset($userinfo['openid'])) { |
|
|
|
// // 获取用户信息失败 |
|
|
|
// echo '获取用户信息失败,请重试'; |
|
|
|
// exit; |
|
|
|
// } |
|
|
|
|
|
|
|
// $_SESSION['openid'] = $openid; |
|
|
|
// echo $openid; |
|
|
|
// header('Location: $referer'); |
|
|
|
// exit; |
|
|
|
$data = json_decode($response, true); |
|
|
|
echo $data; |
|
|
|
if (!$data || !isset($data['access_token']) || !isset($data['openid'])) { |
|
|
|
// 授权失败 |
|
|
|
echo '授权失败,请重试'; |
|
|
|
exit; |
|
|
|
} |
|
|
|
|
|
|
|
// 获取用户信息 |
|
|
|
$access_token = $data['access_token']; |
|
|
|
$openid = $data['openid']; |
|
|
|
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN'; |
|
|
|
$response = file_get_contents($url); |
|
|
|
$userinfo = json_decode($response, true); |
|
|
|
if (!$userinfo || !isset($userinfo['openid'])) { |
|
|
|
// 获取用户信息失败 |
|
|
|
echo '获取用户信息失败,请重试'; |
|
|
|
exit; |
|
|
|
} |
|
|
|
|
|
|
|
$_SESSION['openid'] = $openid; |
|
|
|
echo $openid; |
|
|
|
header('Location: $referer'); |
|
|
|
exit; |
|
|
|
// 用户授权成功,这里可以将用户信息保存到数据库中,或者进行其他操作 |
|
|
|
// var_dump($userinfo); |
|
|
|
|
|
|
|
|