-
Notifications
You must be signed in to change notification settings - Fork 0
/
mhc.php
273 lines (242 loc) · 10.6 KB
/
mhc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* Mini HTTP Client (MHC)
*
* This PHP script provides a minimal HTTP client interface that allows users to make HTTP requests
* (GET, POST, PUT, DELETE) to a specified URL. The script uses cURL to send the requests and formats
* the response for display. It also supports saving previously made requests to local storage for easy reuse.
*
* Features:
* - Send HTTP requests with different methods (GET, POST, PUT, DELETE)
* - Optionally provide a request body for POST and PUT methods
* - Display the response in a formatted and readable way (JSON pretty-print supported)
* - Dark mode toggle for user interface customization
* - Save and reload past requests from local storage (saved in the browser)
*
* @author Kevin Illanas, kevinillanas.dev
* @license GNU GPL v3
* @version 0.1.0
*/
define('AUTHOR', 'Kevin Illanas');
define('LICENSE', 'GNU GPL v3');
define('VERSION', '0.1.0');
$name = $_POST['name'] ?? '';
$url = $_POST['url'] ?? '';
$method = $_POST['method'] ?? 'GET';
$body = $_POST['body'] ?? '';
$response = 'No response has yet been received';
$errores = [];
// Check if the form was submitted
if (isset($_POST['sendpetition'])) {
// Create a new HttpManager instance
$httpManager = new HttpManager($url, $method, ['Content-Type: application/json'], $body);
$response = $httpManager->send();
}
/**
* Class HttpManager
*/
class HttpManager
{
private $url;
private $method;
private $headers;
private $body;
/**
* HttpManager constructor
* @param mixed $url
* @param string $method
* @param array $headers
* @param string $body
* @return void
*/
public function __construct($url, $method = 'GET', $headers = [], $body = '')
{
$this->url = $url;
$this->method = $method;
$this->headers = $headers;
$this->body = $body;
}
/**
* Send the HTTP request
* @return string|bool
*/
public function send()
{
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
if ($this->method === 'POST' && !empty($this->body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
}
$result = curl_exec($ch);
curl_close($ch);
// pretty print JSON
$decoded = json_decode($result);
if ($decoded) {
$result = json_encode($decoded, JSON_PRETTY_PRINT);
}
return $result;
} catch (Exception $e) {
return "Error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini HTTP Client</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap" rel="stylesheet">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📤</text></svg>">
<script src="https://cdn.tailwindcss.com"></script>
<style>
html,
body {
font-family: 'Inter', sans-serif;
height: 100%;
}
pre {
max-width: 100%;
white-space: pre-wrap;
word-wrap: break-word;
overflow: auto;
overflow-wrap: break-word;
word-break: break-all;
}
</style>
</head>
<body>
<div class="flex min-h-full">
<aside class="min-h-full min-w-64 bg-white dark:bg-gray-900 p-6 shadow-md">
<h2 class="text-lg font-semibold mb-4 dark:text-white">History</h2>
<ul id="savedCalls"></ul>
</aside>
<main class="flex flex-col flex-1 p-7 dark:bg-gray-800">
<div class="flex justify-between mb-4">
<h1 class="text-2xl font-semibold dark:text-white">📤 Mini Http Client (MHC)</h1>
<button id="darkModeToggle" class="bg-gray-200 dark:bg-gray-900 text-black dark:text-white px-4 py-2 rounded-md">🌚</button>
</div>
<form id="httpForm" method="POST" action="" class="bg-white dark:bg-gray-900 p-6 rounded-lg shadow-md space-y-4">
<div class="flex space-x-4">
<input type="text" name="name" id="name" placeholder="Request name" value="<?= htmlspecialchars($name) ?>" class="flex-1 p-2 border rounded dark:bg-gray-700 dark:text-white">
</div>
<div class="flex space-x-4">
<select name="method" id="method" class="w-24 p-2 border rounded dark:bg-gray-700 dark:text-white" required>
<option value="GET" <?= $method === 'GET' ? 'selected' : '' ?>>GET</option>
<option value="POST" <?= $method === 'POST' ? 'selected' : '' ?>>POST</option>
<option value="PUT" <?= $method === 'PUT' ? 'selected' : '' ?>>PUT</option>
<option value="DELETE" <?= $method === 'DELETE' ? 'selected' : '' ?>>DELETE</option>
</select>
<input type="url" name="url" id="url" placeholder="URL" value="<?= htmlspecialchars($url) ?>" class="flex-1 p-2 border rounded dark:bg-gray-700 dark:text-white" required>
</div>
<textarea name="body" id="body" placeholder="Request Body (for POST/PUT)" class="w-full h-32 p-2 border rounded dark:bg-gray-700 dark:text-white"><?= htmlspecialchars($body) ?></textarea>
<button type="submit" name="sendpetition" class="w-full bg-blue-500 text-white p-2 rounded hover:bg-blue-600">Send request</button>
</form>
<div class="my-6">
<h2 class="text-lg font-semibold mb-2 dark:text-white">Response:</h2>
<pre id="response" class="bg-white dark:bg-gray-900 dark:text-white shadow-md p-4 rounded-md w-full overflow-x-auto"><?= htmlspecialchars($response) ?></pre>
</div>
<footer class="text-sm text-gray-600 dark:text-gray-400 mt-auto">
<p>Author: <?= AUTHOR ?> - <?= LICENSE ?> - v<?= VERSION ?></p>
</footer>
</main>
</div>
<script>
tailwind.config = {
content: ["./*.html"],
darkMode: "class"
};
</script>
<script>
const body = document.body;
const toggleButton = document.getElementById('darkModeToggle');
const updateDarkMode = (enabled) => {
if (enabled) {
body.classList.add('dark');
toggleButton.textContent = '🌞';
localStorage.setItem('darkMode', 'enabled');
} else {
body.classList.remove('dark');
toggleButton.textContent = '🌚';
localStorage.setItem('darkMode', 'disabled');
}
};
document.addEventListener('DOMContentLoaded', () => {
const darkMode = localStorage.getItem('darkMode') === 'enabled';
updateDarkMode(darkMode);
});
toggleButton.addEventListener('click', () => {
const darkMode = !body.classList.contains('dark');
updateDarkMode(darkMode);
});
</script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('httpForm');
const nameInput = document.getElementById('name');
const methodSelect = document.getElementById('method');
const urlInput = document.getElementById('url');
const bodyTextarea = document.getElementById('body');
const responseArea = document.getElementById('response');
const savedCallsList = document.getElementById('savedCalls');
let savedCalls = JSON.parse(localStorage.getItem('savedCalls')) || [];
function updateSavedCallsList() {
savedCallsList.innerHTML = '';
savedCallsList.className = 'space-y-2';
savedCalls.forEach((call, index) => {
const li = document.createElement('li');
li.className = 'flex justify-between items-center py-1 pl-2 pr-1 bg-white dark:bg-gray-800 dark:text-white rounded-md border dark:border-none rounded hover:bg-gray-50';
const button = document.createElement('button');
button.textContent = call.name || call.url;
button.className = 'text-left flex-grow font-medium truncate';
button.onclick = () => loadCall(index);
const deleteButton = document.createElement('button');
deleteButton.textContent = '✘';
deleteButton.className = 'flex items-center justify-center w-6 h-6 text-gray-400 hover:text-red-600 rounded-md hover:bg-red-100';
deleteButton.onclick = (e) => {
e.stopPropagation();
deleteCall(index);
};
li.appendChild(button);
li.appendChild(deleteButton);
savedCallsList.appendChild(li);
});
}
function saveCall() {
const newCall = {
name: nameInput.value,
method: methodSelect.value,
url: urlInput.value,
body: bodyTextarea.value
};
savedCalls = [newCall, ...savedCalls.filter(call => call.name !== newCall.name)].slice(0, 10);
localStorage.setItem('savedCalls', JSON.stringify(savedCalls));
updateSavedCallsList();
}
function loadCall(index) {
const call = savedCalls[index];
nameInput.value = call.name || '';
methodSelect.value = call.method;
urlInput.value = call.url;
bodyTextarea.value = call.body || '';
}
function deleteCall(index) {
savedCalls.splice(index, 1);
localStorage.setItem('savedCalls', JSON.stringify(savedCalls));
updateSavedCallsList();
}
form.addEventListener('submit', (e) => {
saveCall();
});
updateSavedCallsList();
});
</script>
</body>
</html>