-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetv.php
More file actions
133 lines (114 loc) · 4.1 KB
/
getv.php
File metadata and controls
133 lines (114 loc) · 4.1 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
session_start();
$r = urldecode($_GET['r']) ?? '';
// 如果已登录,重定向到首页
if (isset($_SESSION['responseBody']) && !empty($_SESSION['responseBody'])) {
header('Location: /' . $r);
exit;
}
$username = "";
$token = "";
$authCode = "";
$responseBody = "";
$errorMsg = ""; // 新增:用于存储错误信息
class AuthService {
private $baseUrl = 'http://nlm-api-cn.turtlesim.com/';
public $token;
public $authCode;
public $responseBody;
public function login() {
$identifier = bin2hex(random_bytes(20));
$identifier = str_pad($identifier, 40, '0', STR_PAD_LEFT);
$requestData = [
'Login' => '',
'Password' => '',
'Version' => 2411,
'Device' => [
'Identifier' => $identifier,
'Language' => "Chinese",
],
'Statistic' => null,
];
$headers = [
"Content-Type: application/json",
"Accept: application/json",
"Accept-Language: zh-CN",
];
$response = $this->postRequest("Users/Authenticate", $requestData, $headers);
$statusCode = $response['status'];
$this->responseBody = $response['body'];
$responseData = json_decode($this->responseBody, true);
// 检查JSON解码是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("API响应格式错误: " . json_last_error_msg());
}
// 优先检查业务状态码
if (isset($responseData['Status'])) {
if ($responseData['Status'] === 200) {
$this->token = $responseData['Token'] ?? '';
$this->authCode = $responseData['AuthCode'] ?? '';
return $responseData;
} else {
$errorMsg = $responseData['Message'] ?? '登录失败';
throw new Exception($errorMsg);
}
}
// 处理 HTTP 错误
elseif ($statusCode >= 400) {
$errorMap = [
403 => '访问被拒绝(403)',
404 => '资源不存在(404)',
500 => '服务器错误(500)'
];
$errorMsg = $errorMap[$statusCode] ?? "HTTP 错误: $statusCode";
throw new Exception($errorMsg);
}
// 完全无效的响应
else {
throw new Exception("无效的API响应");
}
}
private function postRequest($endpoint, $data, $headers = []) {
$url = $this->baseUrl . $endpoint;
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
CURLOPT_SSL_VERIFYPEER => true,
]);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("请求失败: $error");
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($response, $headerSize);
curl_close($ch);
return [
'status' => $statusCode,
'body' => $body
];
}
}
try {
$authService = new AuthService();
$loginData = $authService->login();
$_SESSION['token'] = $authService->token;
$_SESSION['authCode'] = $authService->authCode;
$_SESSION['responseBody'] = $authService->responseBody;
// 重定向到首页
header('Location: /' . $r);
exit;
} catch (Exception $e) {
$errorMsg = $e->getMessage();
echo $errorMsg;
}
?>