微信 之网页授权登录

时间:2019-06-11
本文章向大家介绍微信 之网页授权登录,主要包括微信 之网页授权登录使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

index.php

<?php
session_start();
include_once '../wechat.class.php';
$options = include_once '../config.php';
$wechat = new Wechat($options);
if (!$_SESSION['username']) {
    header("location: login.php");
}
if ($_GET['code']) {
    $oauth_info = $wechat->getOauthAccessToken();
    $user_info = $wechat->getOauthUserinfo($oauth_info['access_token'], $oauth_info['openid']);
    $_SESSION['username'] = $user_info['nickname'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页授权</title>
</head>
<body>
<a href="#"><?php echo $_SESSION['username']?>,授权登录成功</a>
<a href="logout.php">退出登录</a>
</body>
</html>

login.php

include_once '../wechat.class.php';
$options = include_once '../config.php';
$wechat = new Wechat($options);

$redirect = "http://www.boyuan.com/api/a/weixin/temp/index.php";
header("location:" . $wechat->getOauthRedirect($redirect));

config.php

return $options = array(
    'token' => 'vEeApDmLdwX7UnX7GL', //填写你设定的key
    'encodingaeskey' => 'znyEPja2gZQ71TguwWIXGVhAeaaja5AGdTgncu0b6d3', //填写加密用的EncodingAESKey
    'appid' => 'wxdede5853fa06c39a', //填写高级调用功能的app id
    'appsecret' => 'eb4d5dcc51b9d757337317aa5ddfdcf5', //填写高级调用功能的密钥
    'debug' => true,
    'logcallback' => 'write_log'
);

wechat.class.php的获取地址如下

链接: https://pan.baidu.com/s/14SFri1PyeXbdWUa8DsR0QQ 提取码: h5o0

效果如图:

原文地址:https://www.cnblogs.com/yang-2018/p/11003133.html