1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class WxProxy |
4
|
|
|
* @Author: TinyMeng <[email protected]> |
5
|
|
|
* @Created: 2018/11/4 |
6
|
|
|
*/ |
7
|
|
|
class WxProxy |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
protected $AuthorizeURL = 'https://open.weixin.qq.com'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @Author: TinyMeng <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
public function run(){ |
16
|
|
|
if (isset($_GET['code'])) { |
17
|
|
|
$state = isset($_GET['state']) ? $_GET['state'] : ""; |
18
|
|
|
header('Location: ' . $_COOKIE['redirect_uri'] . '?code=' . $_GET['code'] . '&state=' . $state); |
19
|
|
|
} else { |
20
|
|
|
if(!isset($_GET['appid']) || !isset($_GET['response_type']) || !isset($_GET['scope'])){ |
21
|
|
|
echo "参数缺失"; |
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
$state = isset($_GET['state']) ? $_GET['state'] : ""; |
25
|
|
|
|
26
|
|
|
$protocol = $this->is_HTTPS() ? 'https://' : 'http://'; |
27
|
|
|
$params = array( |
28
|
|
|
'appid' => $_GET['appid'], |
29
|
|
|
'redirect_uri' => $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['DOCUMENT_URI'], |
30
|
|
|
'response_type' => $_GET['response_type'], |
31
|
|
|
'scope' => $_GET['scope'], |
32
|
|
|
'state' => $state, |
33
|
|
|
); |
34
|
|
|
if($_GET['scope'] == 'snsapi_login'){ |
35
|
|
|
//扫码登录 |
36
|
|
|
$AuthorizeURL = $this->AuthorizeURL . '/connect/qrconnect'; |
37
|
|
|
}else{ |
38
|
|
|
$AuthorizeURL = $this->AuthorizeURL . '/connect/oauth2/authorize'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
setcookie('redirect_uri', urldecode($_GET['redirect_uri']), $_SERVER['REQUEST_TIME'] + 60, '/'); |
42
|
|
|
header('Location: ' . $AuthorizeURL . '?' . http_build_query($params) . '#wechat_redirect'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 是否https |
48
|
|
|
* @Author: TinyMeng <[email protected]> |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
protected function is_HTTPS(){ |
52
|
|
|
if (!isset($_SERVER['HTTPS'])) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
if ($_SERVER['HTTPS'] === 1) { //Apache |
56
|
|
|
return true; |
57
|
|
|
} elseif ($_SERVER['HTTPS'] === 'on') { //IIS |
58
|
|
|
return true; |
59
|
|
|
} elseif ($_SERVER['SERVER_PORT'] == 443) { //其他 |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$app = new WxProxy(); |
67
|
|
|
$app->run(); |