CCSWebsite/console/ins/proxy.php
2025-06-17 01:43:15 +00:00

88 lines
2.4 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* MCSManager API 代理
* 用于转发前端请求到 MCSManager 后端 API避免跨域问题
*/
// 配置信息 - 请根据实际情况修改
$config = [
'api_base_url' => 'http://154.222.30.227:23333/api', // 请替换为实际的 API 基础 URL
'api_key' => 'c507ddc820254ea8a0ea8f279a961b01', // 重要:请替换为实际 API Key
'timeout' => 10,
];
// 处理预检请求
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit;
}
// 获取请求路径
$path = $_GET['path'] ?? '';
unset($_GET['path']);
// 构建API URL自动添加api_key参数
$url = "{$config['api_base_url']}/{$path}?apikey={$config['api_key']}";
// 添加GET参数如果有
if (!empty($_GET)) {
$url .= '&' . http_build_query($_GET);
}
// 初始化CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $config['timeout']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// 添加自定义请求头
$headers = [
'X-Requested-With: XMLHttpRequest', // 添加此行
'Content-Type: application/json', // 确保 JSON 格式请求头
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 处理POST请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
// 根据Content-Type处理请求体
$contentType = $_SERVER['CONTENT_TYPE'] ?? '';
if (strpos($contentType, 'application/json') !== false) {
// JSON格式请求体
$requestBody = file_get_contents('php://input');
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($headers, [
'Content-Length: ' . strlen($requestBody)
]));
} else {
// 表单格式请求体
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
}
}
// 执行请求
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
// 处理响应
if ($error) {
http_response_code(500);
echo json_encode([
'code' => 500,
'message' => '代理请求失败',
'error' => $error,
'api_url' => $url // 添加请求的地址
]);
} else {
http_response_code($httpCode);
// 将请求的地址添加到响应中
$responseJson = json_decode($response, true);
echo $response;
}
exit;
?>