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, PATC.........完整代码请登录后点击上方下载按钮下载查看















网友评论0