<?php
/**
* @desc 实现微信网页授权回调域名的代理转发,突破回调域名只能应用于一个项目的限制。
* usage:
* 代理域名:http://proxy.domain.com/weixin_redirect.php
* 授权域名1:http://one.domain.com
* 授权域名2:http://two.domain.com
*
* 域名1发起授权:
* 1、获取code:http://proxy.domain.com/weixin_redirect.php?appid={$appid}&scope=snsapi_login&state={$state}&redirect_uri={$redirect_uri}&。={$device}
*/
// COOKIES 设置
define('COOKIES_PATH', '/');
define('COOKIES_EXPIRES', gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT", time() + 60));
// 代理设置 (微信中只需要配置一次代理转发)
define('PROXY_DOMAIN_URI', 'cdn.51wantui.com');
define('PROXY_REDIRECT_URI', PROXY_DOMAIN_URI . '/weixin_redirect.php?method=yes');
// 地址设置
define('DOMAIN_URI', 'cdn.51wantui.com');
define('REDIRECT_URI', 'http://cdn.51wantui.com/weixin_redirect.php?method=yes');
$method = $_GET['method'];
if (isset($method) && $method == 'checksign') {
check_signature();
exit();
}
if (isset($method) && $method == 'yes') {
var_dump($_GET);
exit();
}
// ---------------------------------------------------------------------------------------------------------------------
$appid = '';
$scope = 'snsapi_login'; // 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
$state = '';
$code = '';
$redirect_uri = 'http://cdn.51wantui.cn/weixin_redirect.php?method=yes'; // 真实回调地址
$device = '';
$protocol = '';
// 支持授权回调的域名
$redirectUrlConfig = [
'http://cdn.51wantui.com',
'http://cdn.51wantui.cn',
// 'https://three.domain.com/s?wd=codetoany&ie=utf-8', // 支持携带参数
];
if (is_https()) {
$protocol = 'https';
} else {
$protocol = 'http';
}
// 因为微信pc端跟移动端的授权地址是不一样的,所以需要多加个参数告诉它在转发给授权申请给微信的时候,是用PC端还是移动端的授权地址。
if (isset($_GET['device'])) {
$device = $_GET['device'];
}
if (isset($_GET['appid'])) {
$appid = $_GET['appid'];
}
if (isset($_GET['state'])) {
$state = $_GET['state'];
}
if (isset($_GET['redirect_uri'])) {
$redirect_uri = $_GET['redirect_uri'];
}
if (isset($_GET['code'])) {
$code = $_GET['code'];
}
if (isset($_GET['scope'])) {
$scope = $_GET['scope'];
}
if ($code == 'test') {
exit;
}
if (empty($code)) {
$authUrl = '';
if ($device == 'pc') {
$authUrl = 'https://open.weixin.qq.com/connect/qrconnect';
} else {
$authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize';
}
$options = [
$authUrl,
'?appid=' . $appid,
'&redirect_uri=' . urlencode($protocol . '://' . $_SERVER['HTTP_HOST'] . '/' . 'weixin_redirect.php'), // 代理回调地址
'&response_type=code',
'&scope=' . $scope,
'&state=' . $state,
'#wechat_redirect'
];
//把redirect_uri先写到cookie
header(implode('', [
"Set-Cookie: redirect_uri=",
urlencode($redirect_uri),
"; path=/",
// "; domain=" . get_domain(),
"; domain=cdn.51wantui.com",
"; expires=" . gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT", time() + 3600),
"; Max-Age=" . 3600,
"; httponly"
]));
// header("Set-Cookie: redirect_uri=" . urlencode($redirect_uri) . "; path=/; domain=" . get_domain() . "; expires=" . gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT", time() + 9600));
// setcookie("redirect_uri", urlencode($redirect_uri), time() + 60);
header('Location: ' . implode('', $options));
} else {
if (isset($_COOKIE['redirect_uri'])) {
$back_url = urldecode($_COOKIE['redirect_uri']);
header('Location: ' . implode('', [
$back_url,
strpos($back_url, '?') ? '&' : '?',
'code=' . $code,
'&state=' . $state,
'&red=' . mt_rand(1, 9) . '_' . mt_rand(100, 999)
]));
} else {
echo 'cookie false';
}
}
/**
* 接口配置信息
* @return false
*/
function check_signature()
{
if (!isset($_GET['signature']) || !isset($_GET['nonce']) || !isset($_GET['timestamp'])) {
return false;
}
// 先获取到这三个参数
$signature = $_GET['signature'];
$nonce = $_GET['nonce'];
$timestamp = $_GET['timestamp'];
$token = 'iaPYBuoJISy4dKEn';
// 把这三个参数存到一个数组里面
$tmpArr = array($timestamp, $nonce, $token);
// 进行字典排序
sort($tmpArr, SORT_STRING);
// 把数组中的元素合并成字符串,impode()函数是用来将一个数组合并成字符串的
$tmpStr = implode($tmpArr);
// sha1加密,调用sha1函数
$tmpStr = sha1($tmpStr);
// 判断加密后的字符串是否和signature相等
if ($tmpStr == $signature) {
echo isset($_GET['echostr']) ? $_GET['echostr'] : "success";
exit();
}
}
/**
* @desc 判断是否为 https 协议
* @return bool
*/
function is_https()
{
if (!isset($_SERVER['HTTPS'])) return false;
if ($_SERVER['HTTPS'] === 1) { // Apache
return true;
} elseif ($_SERVER['HTTPS'] === 'on') { // IIS
return true;
} elseif ($_SERVER['SERVER_PORT'] == 443) { // 其他
return true;
}
return false;
}
/**
* @desc 获取域名
* @return false|mixed|string
*/
function get_domain()
{
$server_name = $_SERVER['SERVER_NAME'];
if (strpos($server_name, 'www.') !== false) {
return substr($server_name, 4);
}
return $server_name;
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部