'Missing target URL parameter']); exit; } // 测试API调用 if (isset($_GET['test']) && $_GET['test'] == '1') { $test_body = isset($_POST['test_body']) ? $_POST['test_body'] : ''; $test_headers = isset($_POST['test_headers']) ? $_POST['test_headers'] : ''; if (empty($test_body) || empty($test_headers)) { echo "缺少测试数据"; exit; } // 解析JSON数据 $body_data = json_decode($test_body, true); $headers_data = json_decode($test_headers, true); echo "
URL: $url
方法: POST
";
foreach ($headers_data as $name => $value) {
// 隐藏部分token以保护安全
if ($name === 'Authorization' && strlen($value) > 20) {
$maskedValue = substr($value, 0, 10) . '...' . substr($value, -10);
echo "$name: $maskedValue\n";
} else {
echo "$name: $value\n";
}
}
echo "
" . json_encode($body_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "
HTTP状态码: = 200 && $info['http_code'] < 300 ? 'success' : 'error') . "'>" . $info['http_code'] . "
"; echo "响应时间: " . $info['total_time'] . " 秒
"; echo "";
$headerLines = explode("\r\n", $header);
foreach ($headerLines as $line) {
if (!empty($line)) {
echo htmlspecialchars($line) . "\n";
}
}
echo "";
// 检查是否是流式响应
$isStream = isset($body_data['stream']) && $body_data['stream'] === true;
if ($isStream) {
echo "解析流式数据...
"; echo "";
if ($json_response !== null) {
echo json_encode($json_response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} else {
echo htmlspecialchars($body);
}
echo "";
}
} catch (Exception $e) {
echo "" . $e->getMessage() . "
"; // 提供更多调试帮助 echo "PHP版本: " . phpversion() . "
"; echo "目标URL: " . $url . "
"; echo "请求方法: " . $_SERVER['REQUEST_METHOD'] . "
"; echo "";
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
echo "";
if (!empty($headers_data)) {
echo "";
foreach ($headers_data as $name => $value) {
echo "$name: $value\n";
}
echo "";
}
echo "";
echo file_get_contents('php://input');
echo "";
if (!empty($body_data)) {
echo "";
echo json_encode($body_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
echo "";
}
// 如果提供了完整数据,显示测试按钮
if (!empty($debug_body) && !empty($debug_headers)) {
echo "";
}
exit;
}
// 读取请求体
$postData = file_get_contents('php://input');
try {
// 检查cURL是否可用
if (!function_exists('curl_init')) {
throw new Exception('PHP cURL扩展未启用,请在php.ini中启用cURL扩展');
}
// 设置cURL选项
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// 处理请求方法
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
// 设置请求头
$headers = [];
foreach (getallheaders() as $name => $value) {
if ($name != 'Host' && $name != 'Origin' && $name != 'Referer') {
$headers[] = "$name: $value";
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 执行请求
$response = curl_exec($ch);
// 检查错误
if ($response === false) {
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
throw new Exception("cURL错误($errno): $error");
}
$info = curl_getinfo($ch);
curl_close($ch);
// 设置响应头
if (isset($info['content_type'])) {
header('Content-Type: ' . $info['content_type']);
}
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
// 处理OPTIONS请求(预检请求)
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('HTTP/1.1 200 OK');
exit;
}
// 输出响应
echo $response;
} catch (Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
header('Content-Type: application/json');
echo json_encode(['error' => $e->getMessage()]);
}
exit;
}
// 添加PHP检测
$isPhp = (strpos(strtolower($_SERVER['SERVER_SOFTWARE'] ?? ''), 'php') !== false) ||
(function_exists('php_sapi_name') && php_sapi_name() !== false);
?>