620e7ce21c
修改url.txt和auth.json的路径从绝对路径改为相对路径,提高代码的可移植性和部署灵活性
48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
// 设置响应头,允许所有来源的跨域请求
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
|
header('Content-Type: application/json');
|
|
|
|
// 处理OPTIONS预检请求
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
exit(0);
|
|
}
|
|
|
|
// 定义文件路径
|
|
$urlFilePath = 'url.txt';
|
|
$authFilePath = 'auth.json';
|
|
|
|
// 读取并返回auth.json文件的内容
|
|
if (file_exists($authFilePath)) {
|
|
// 读取JSON文件内容
|
|
$jsonContent = file_get_contents($authFilePath);
|
|
|
|
// 验证JSON格式是否正确
|
|
if ($jsonContent && json_decode($jsonContent) !== null) {
|
|
// 直接输出JSON内容
|
|
echo $jsonContent;
|
|
exit;
|
|
} else {
|
|
// JSON格式错误时的响应
|
|
$errorResponse = array(
|
|
'status' => 'error',
|
|
'message' => 'JSON文件格式错误或内容为空'
|
|
);
|
|
echo json_encode($errorResponse, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
} else {
|
|
// 文件不存在时的响应
|
|
$errorResponse = array(
|
|
'status' => 'error',
|
|
'message' => 'auth.json文件不存在'
|
|
);
|
|
echo json_encode($errorResponse, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
// 终止脚本执行
|
|
exit;
|
|
?>
|