php实现支持亮色暗色类似postman的api调试工具代码
代码语言:php
所属分类:其他
代码描述:php实现支持亮色暗色类似postman的api调试工具代码
代码标签: php 亮色 暗色 类似 postman api 调试 工具 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php
// api_tester.php
// A Postman-like API testing tool in a single PHP file.
// Compatible with PHP 5.6+ and modern browsers.
// --- START: API Proxy Logic ---
// When the page is POSTed to with a `?proxy=1` query string, it acts as a proxy.
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['proxy'])) {
// The proxy always returns JSON.
header('Content-Type: application/json');
// Get the request details from the Vue app's POST body.
$raw_input = file_get_contents('php://input');
$request_data = json_decode($raw_input, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode(array('error' => 'Proxy Error: Invalid JSON sent from client.'));
exit;
}
// Extract details for the cURL request.
$method = isset($request_data['method']) ? strtoupper($request_data['method']) : 'GET';
$url = isset($request_data['url']) ? $request_data['url'] : '';
$body_data = isset($request_data['body']) ? $request_data['body'] : null;
$headers_from_client = isset($request_data['headers']) && is_array($request_data['headers']) ? $request_data['headers'] : array();
if (empty($url)) {
http_response_code(400);
echo json_encode(array('error' => 'Proxy Error: Request URL is missing.'));
exit;
}
// Initialize cURL.
$ch = curl_init();
// Convert the client's header array to the format cURL expects.
$curl_headers = array();
foreach ($headers_from_client as $header) {
if (!empty($header['key'])) {
$curl_headers[] = $header['key'] . ': ' . $header['value'];
}
}
// Set cURL options.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string.
curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in the response.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 30-second timeout.
// In development, you might need to bypass SSL verification for local servers.
// Use with caution in production.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Handle request body for POST, PUT, PATCH.
if (!is_null($body_data) && in_array($method, array('POST', 'PUT', 'PATCH'))) {
$json_body = json_encode($body_data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_body);
// Add Content-Type header for JSON, but don't override if the user set it manually.
$has_content_type = false;
foreach($curl_headers as $h) {
if (stripos($h, 'Content-Type:') === 0) {
$has_content_type = true;
break;
}
}
if (!$has_content_type) {
$curl_headers[] = 'Content-Type: application/json';
}
$curl_headers[] = 'Content-Length: ' . strlen($json_body);
}
if (!empty($curl_headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
}
// Execute the request and measure time.
$start_time = microtime(true);
$response_raw = curl_exec($ch);
$end_time = microtime(true);
$request_time = round(($end_time - $start_time) * 1000); // Time in milliseconds.
$curl_error = curl_error($ch);
if ($curl_error) {
http_response_code(502); // Bad Gateway, indicates a proxy-level failure.
echo json_encode(array('error' => 'cURL Error: ' . $curl_error));
curl_close($ch);
exit;
}
// Separate response headers from the body.
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response_headers_raw = substr($response_raw, 0, $header_size);
$response_body_raw = substr($response_raw, $header_size);
$response_size = strlen($response_bod.........完整代码请登录后点击上方下载按钮下载查看















网友评论0