Last active
December 23, 2024 15:22
-
-
Save miceno/7f2517b3d242cd7c0aeac06a685566e7 to your computer and use it in GitHub Desktop.
PHP script to mock a response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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