From 7a874c8bfe7304b6c6527965b0d0610ce685e01a Mon Sep 17 00:00:00 2001 From: wsh5485 Date: Tue, 14 Jan 2025 01:21:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 53 ++++++++++++++ config.php | 9 +++ index.php | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 262 insertions(+) create mode 100644 README.md create mode 100644 config.php create mode 100644 index.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b2f37c --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# API 调用工具 + +## 项目概述 +这是一个用于批量调用API并实时显示调用结果的PHP工具。使用SSE(Server-Sent Events)技术实现实时进度更新和结果展示。 + +## 配置说明 +1. 修改 `config.php` 文件: + - 在 `$links` 数组中添加或修改需要调用的API地址 + - 示例: + ```php + $links = [ + 'https://api.example.com/endpoint1', + 'https://api.example.com/endpoint2' + ]; + ``` + +## 使用说明 +1. 将项目部署到支持PHP的Web服务器 +2. 访问 `index.php` +3. 点击"执行 API 调用"按钮 +4. 页面将显示: + - 实时进度条 + - 每个API调用的详细信息: + - 调用链接 + - 开始时间 + - 结束时间 + - 执行时间 + - 返回结果 + +## 修改说明 +1. 修改API调用超时时间: + - 打开 `index.php` + - 修改以下参数: + ```php + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 连接超时时间(秒) + curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 执行超时时间(秒) + ``` + +2. 修改SSE心跳间隔: + - 打开 `index.php` + - 查找 `setInterval` 函数 + - 修改5000为期望的间隔时间(毫秒) + +3. 修改重试次数: + - 打开 `index.php` + - 查找 `maxReconnectAttempts` 变量 + - 修改值为期望的重试次数 + +## 注意事项 +1. 确保服务器支持SSE +2. 确保PHP版本 >= 5.3 +3. 如果使用Nginx,请确保配置了正确的缓冲设置 +4. 生产环境建议启用SSL验证 diff --git a/config.php b/config.php new file mode 100644 index 0000000..a5eec85 --- /dev/null +++ b/config.php @@ -0,0 +1,9 @@ + $error_msg]); + } + curl_close($ch); + return $response; +} + +if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'text/event-stream') !== false) { + // 设置SSE头信息 + header('Content-Type: text/event-stream; charset=UTF-8'); + header('Cache-Control: no-cache, must-revalidate'); + header('Connection: keep-alive'); + header('X-Accel-Buffering: no'); // 禁用Nginx缓冲 + header('Access-Control-Allow-Origin: *'); + header('Content-Encoding: none'); // 禁用压缩 + + // 设置脚本超时时间 + set_time_limit(0); + ignore_user_abort(true); + + // 禁用输出缓冲 + if (ob_get_level()) { + ob_end_clean(); + } + ob_implicit_flush(true); + + // 发送心跳包保持连接 + function sendHeartbeat() { + echo ": heartbeat\n\n"; + ob_flush(); + flush(); + } + + $total = count($links); + $completed = 0; + + // 发送初始心跳 + sendHeartbeat(); + + // 检查连接是否中断 + if (connection_aborted()) { + exit; + } + + foreach ($links as $link) { + $startTime = microtime(true); + $result = callApi($link); + $endTime = microtime(true); + $executionTime = round(($endTime - $startTime) * 1000, 2); // 毫秒 + + $completed++; + $progress = round(($completed / $total) * 100); + + $data = [ + 'link' => $link, + 'start_time' => date('Y-m-d H:i:s', $startTime), + 'end_time' => date('Y-m-d H:i:s', $endTime), + 'execution_time' => $executionTime, + 'result' => $result, + 'progress' => $progress + ]; + + echo "data: " . json_encode($data) . "\n\n"; + ob_flush(); + flush(); + } + + echo "event: complete\ndata: {}\n\n"; + exit; +} +?> + + + + API 调用工具 + + + +

API 调用工具

+ +
+
0%
+
+
+ + + +