Skip to content

Instantly share code, notes, and snippets.

@miceno
Last active December 23, 2024 15:22
Show Gist options
  • Save miceno/7f2517b3d242cd7c0aeac06a685566e7 to your computer and use it in GitHub Desktop.
Save miceno/7f2517b3d242cd7c0aeac06a685566e7 to your computer and use it in GitHub Desktop.
PHP script to mock a response
<?php
function getRequestHeaders() {
$headers = array();
foreach($_SERVER as $key => $value) {
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower($key))));
$headers[$header] = $value;
}
return $headers;
}
$file_to_serve = basename($_GET['filename']) ?? (http_response_code(404) || die());
$header_text = "";
if ($_GET['debug']) {
$headers = getRequestHeaders();
$header_text .= "<br/>";
foreach ($headers as $name => $value) {
$header_text .= "$name: $value<br />\n";
}
}
$handle = fopen($file_to_serve . ".header", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
header($line);
}
fclose($handle);
} else {
http_response_code(404);
die();
}
$handle = fopen($file_to_serve . ".json", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
print($line);
}
fclose($handle);
}
print($header_text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment