-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
158 lines (139 loc) · 4.85 KB
/
api.php
File metadata and controls
158 lines (139 loc) · 4.85 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
require 'config.php';
// 设置默认字符集
header('Content-Type: application/json; charset=utf-8');
// 允许跨域请求(CORS)
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
// 获取请求参数
$action = $_GET['action'] ?? 'get';
$format = $_GET['format'] ?? 'json'; // 可选: json, text, html
$encode = $_GET['encode'] ?? 'unicode'; // 可选: unicode, plain
$callback = $_GET['callback'] ?? null; // JSONP回调函数名
// 处理API请求
switch ($action) {
case 'get':
get_hitokoto($format, $encode, $callback);
break;
case 'count':
get_hitokoto_count($format, $encode, $callback);
break;
default:
handle_error(400, '无效操作', $format, $callback);
exit;
}
// 获取随机一言
function get_hitokoto($format, $encode, $callback) {
$conn = db_connect();
$sql = "SELECT content FROM " . DB_TABLE . " ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
$row = $result->fetch_assoc();
$content = $row['content'];
// 处理编码
if ($encode === 'plain') {
$content = urlencode($content);
}
// 根据格式返回
switch ($format) {
case 'text':
header('Content-Type: text/plain; charset=utf-8');
echo $content;
break;
case 'html':
header('Content-Type: text/html; charset=utf-8');
echo "<blockquote>$content</blockquote>";
break;
case 'json':
default:
$response = [
'code' => 200,
'msg' => 'success',
'data' => $content
];
// 处理JSONP
if ($callback && preg_match('/^\w+$/', $callback)) {
header('Content-Type: application/javascript; charset=utf-8');
echo $callback . '(' . json_encode($response) . ');';
} else {
echo json_encode($response, JSON_UNESCAPED_UNICODE);
}
break;
}
} else {
handle_error(404, '未找到任何内容', $format, $callback);
}
$conn->close();
}
// 获取一言总数
function get_hitokoto_count($format, $encode, $callback) {
$conn = db_connect();
$sql = "SELECT COUNT(*) AS total FROM " . DB_TABLE;
$result = $conn->query($sql);
if ($result) {
$row = $result->fetch_assoc();
$count = (int)$row['total'];
// 根据格式返回
switch ($format) {
case 'text':
header('Content-Type: text/plain; charset=utf-8');
echo $count;
break;
case 'html':
header('Content-Type: text/html; charset=utf-8');
echo "<p>当前共有 {$count} 条一言</p>";
break;
case 'json':
default:
$response = [
'code' => 200,
'msg' => 'success',
'data' => $count
];
// 处理JSONP
if ($callback && preg_match('/^\w+$/', $callback)) {
header('Content-Type: application/javascript; charset=utf-8');
echo $callback . '(' . json_encode($response) . ');';
} else {
echo json_encode($response);
}
break;
}
} else {
handle_error(500, '数据库查询失败', $format, $callback);
}
$conn->close();
}
// 错误处理函数
function handle_error($code, $message, $format, $callback) {
switch ($format) {
case 'text':
header('Content-Type: text/plain; charset=utf-8');
http_response_code($code);
echo $message;
break;
case 'html':
header('Content-Type: text/html; charset=utf-8');
http_response_code($code);
echo "<div class='error'>$message</div>";
break;
case 'json':
default:
$response = [
'code' => $code,
'msg' => $message
];
// 处理JSONP
if ($callback && preg_match('/^\w+$/', $callback)) {
header('Content-Type: application/javascript; charset=utf-8');
echo $callback . '(' . json_encode($response) . ');';
} else {
header('Content-Type: application/json; charset=utf-8');
http_response_code($code);
echo json_encode($response);
}
break;
}
}
?>