-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-code.php
52 lines (38 loc) · 1.39 KB
/
get-code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
if (empty($_GET['redirect_uri']) && empty($_GET['code'])) {
header('status: 422'); die('<h1>:(</h1>');
}
session_start();
if (!empty($_GET['code'])) {
if (empty($_SESSION['redirect_uri'])) {
header('status: 422'); die('<h1>:(</h1>');
}
$parts = parse_url($_SESSION['redirect_uri']);
session_unset();
parse_str($parts['query'], $vars);
$vars['code'] = $_GET['code'];
$redirectUri = sprintf('%s://%s%s%s?%s',
$parts['scheme'], $parts['host'],
isset($parts['port']) ? sprintf(':%s', $parts['port']) : '',
$parts['path'],
http_build_query($vars)
);
header(sprintf('Location: %s', $redirectUri)); die();
}
$hosts = array(
'wechat.xxx.com',
'weixin.xxx.com',
);
$parts = parse_url($_GET['redirect_uri']);
if (!in_array($parts['host'], $hosts)) {
header('status: 422'); die('<h1>:(</h1>');
}
$_SESSION['redirect_uri'] = $_GET['redirect_uri'];
$queries = array(
'appid' => 'wx__________', // change it
'redirect_uri' => sprintf('http%s://%s%s', isset($_SERVER['HTTPS']) ? 's' : '', $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']),
'response_type' => 'code',
'scope' => empty($_GET['scope']) ? 'snsapi_base' : $_GET['scope'],
);
$urlGetCode = sprintf('https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect', http_build_query($queries));
header(sprintf('Location: %s', $urlGetCode));