first commit
This commit is contained in:
50
api/api.php
Normal file
50
api/api.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
try {
|
||||
require_once __DIR__.'/../includes/config.php';
|
||||
require_once __DIR__.'/../includes/db.php';
|
||||
require_once __DIR__.'/../includes/api_functions.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// 获取并验证参数
|
||||
$params = validateRequestParams();
|
||||
|
||||
// 构建查询语句
|
||||
$query = buildImageQuery($params);
|
||||
|
||||
// 获取随机图片
|
||||
$image = fetchRandomImage($pdo, $query, $params);
|
||||
|
||||
// 处理结果
|
||||
if ($image) {
|
||||
// 直接跳转模式
|
||||
if (isset($_GET['redirect']) && $_GET['redirect'] === 'true') {
|
||||
// 验证URL有效性
|
||||
if (filter_var($image['url'], FILTER_VALIDATE_URL)) {
|
||||
header("Location: " . $image['url']);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo jsonError('无效的图片URL');
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
// 正常JSON响应
|
||||
echo jsonResponse([
|
||||
'url' => $image['url'],
|
||||
'resolution' => $params['resolution'],
|
||||
'category' => $params['category']
|
||||
]);
|
||||
} else {
|
||||
echo jsonError('未找到匹配的图片', 404);
|
||||
}
|
||||
|
||||
} catch (InvalidArgumentException $e) {
|
||||
echo jsonError($e->getMessage(), 400);
|
||||
} catch (PDOException $e) {
|
||||
error_log("Database Error: " . $e->getMessage());
|
||||
echo jsonError('数据库查询失败', 500);
|
||||
} catch (Exception $e) {
|
||||
error_log("System Error: " . $e->getMessage());
|
||||
echo jsonError('系统繁忙,请稍后再试', 500);
|
||||
}
|
||||
Reference in New Issue
Block a user