php实现系统所有api集合在线调试工具代码
代码语言:php
所属分类:其他
代码描述:php实现系统所有api集合在线调试工具代码
代码标签: php 系统 所有 api 集合 在线 调试 工具 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php
// api_tester.php
$target_url ="https://example.com/v1";//修改基础url地址
// --- START: API Proxy Logic ---
// 如果请求中包含 'proxy' 参数,则作为API代理执行
if (isset($_GET['proxy'])) {
// 开启 session,以便转发 cookie
session_start();
// --- 自动检测API前缀 ---
// 这个前缀是真实API服务器的地址
$scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$host = $_SERVER['HTTP_HOST'];
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$api_prefix = "{$scheme}://{$host}{$path}/";
// 获取前端传来的API路径和方法
$api_path = $_GET['path'] ?? '';
$api_method = $_GET['method'] ?? 'GET';
//$target_url = $api_prefix . $api_path;
// 获取前端发来的请求体
$request_body = file_get_contents('php://input');
// 初始化 cURL
$ch = curl_init();
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应体,而不是直接输出
curl_setopt($ch, CURLOPT_HEADER, true); // 返回响应头
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $api_method); // 设置请求方法
// 如果是 POST 或 PUT, 添加请求体和 Content-Type 头
if (in_array($api_method, ['POST', 'PUT']) && !empty($request_body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($request_body)
]);
}
// *** 关键:转发浏览器的 session cookie ***
// 这使得API能够识别用户的登录状态
if (isset($_COOKIE[session_name()])) {
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . $_COOKIE[session_name()]);
}
// 执行 cURL 请求
$response = curl_exec($ch);
$error = curl_error($ch);
// 如果 cURL 出错,返回错误信息
if ($error) {
header('Content-Type: application/json');
http_response_code(500); // 代理服务器本身错误
echo json_encode(['error' => 'Proxy cURL Error: ' . $error]);
curl_close($ch);
exit;
}
// 分离响应头和响应体
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_str = substr($response, 0, $header_size);
$body_str = substr($response, $header_size);
// 获取真实API的HTTP状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL
curl_close($ch);
// *** 关键:将API服务器设置的cookie(如Set-Cookie)转发回浏览器 ***
$headers = explode("\r\n", $header_str);
foreach ($headers as $header) {
// 只转发 Set-Cookie 和 Content-Type 头
if (stripos($header, 'Set-Cookie:') === 0 || stripos($header, 'Content-Type:') === 0) {
header($header, false);
}
}
// 构造返回给前端的JSON
$response_for_frontend = [
'status' => $status_code,
'body' => json_decode($body_str, true) ?? $body_str, // 尝试解码JSON,失败则返回原始字符串
'headers' => $headers
];
// 代理本身总是返回 200 OK,实际状态码在JSON负载中
http_response_code(200);
if(strpos(strtolower(implode("\n", $headers)), 'content-type: application/json') === false) {
header('Content-Type: application/json');
}
echo json_encode($response_for_frontend);
// 代理任务完成,退出脚本,不渲染下面的HTML
exit;
}
// --- END: API Proxy Logic ---
// --- API 定义 ---
// 在这里定义所有需要测试的API接口
// 'auth' => 'none' (无需登录), 'user' (需用户登录), 'technician' (需技师登录), 'admin' (需管理员登录)
$api_list = [
'身份认证' => [
['name' => '用户注册', 'method' => 'POST', 'url' => 'api/auth/register', 'auth' => 'none', 'body' => '{"username": "newuser", "email": "newuser@example.com", "password": "password"}'],
['name' => '用户登录', 'method' => 'POST', 'url' => 'api/auth/login', 'auth' => 'none', 'body' => '{"email": "user@example.com", "password": "password"}'],
['name' => '检查用户登录状态', 'method' => 'GET', 'url' => 'api/auth/check', 'auth' => 'none', 'body' => ''],
['name' => '技师注册', 'method' => 'POST', 'url' => 'api/auth/technician/register', 'auth' => 'none', 'body' => '{"username": "newtech", "email": "newtech@example.com", "password": "password", "real_name": "王师傅"}'],
['name' => '技师登录', 'method' => 'POST', 'url' => 'api/auth/technician/login', 'auth' => 'none', 'body' => '{"email": "tech01@example.com", "password": "password"}'],
['name' => '检查技师登录状态', 'method' => 'GET', 'url' => 'api/auth/technician/check', 'auth' => 'none', 'body' => ''],
['name' => '管理员登录', 'method' => 'POST', 'url' => 'api/auth/admin/login', 'auth' => 'none', 'body' => '{"username": "admin", "password": "password"}'],
['name' => '检查管理员登录状态', 'method' => 'GET', 'url' => 'api/auth/admin/check', 'auth' => 'none', 'body' => ''],
['name' => '用户/技师/管理员登出', 'method' => 'POST', 'url' => 'api/auth/logout', 'auth'.........完整代码请登录后点击上方下载按钮下载查看















网友评论0