Last active
December 1, 2024 09:58
-
-
Save magnetikonline/650e30e485c0f91f2f40 to your computer and use it in GitHub Desktop.
PHP script to dump full HTTP request to file (method, HTTP headers and body).
This file contains 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 | |
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40 | |
class DumpHTTPRequestToFile { | |
public function execute($targetFile) { | |
$data = sprintf( | |
"%s %s %s\n\nHTTP headers:\n", | |
$_SERVER['REQUEST_METHOD'], | |
$_SERVER['REQUEST_URI'], | |
$_SERVER['SERVER_PROTOCOL'] | |
); | |
foreach ($this->getHeaderList() as $name => $value) { | |
$data .= $name . ': ' . $value . "\n"; | |
} | |
$data .= "\nRequest body:\n"; | |
file_put_contents( | |
$targetFile, | |
$data . file_get_contents('php://input') . "\n" | |
); | |
echo("Done!\n\n"); | |
} | |
private function getHeaderList() { | |
$headerList = []; | |
foreach ($_SERVER as $name => $value) { | |
if (preg_match('/^HTTP_/',$name)) { | |
// convert HTTP_HEADER_NAME to Header-Name | |
$name = strtr(substr($name,5),'_',' '); | |
$name = ucwords(strtolower($name)); | |
$name = strtr($name,' ','-'); | |
// add to list | |
$headerList[$name] = $value; | |
} | |
} | |
return $headerList; | |
} | |
} | |
(new DumpHTTPRequestToFile)->execute('./dumprequest.txt'); |
This file contains 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
GET /dumprequest.php HTTP/1.1 | |
HTTP headers: | |
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8 | |
Accept-Encoding: gzip, deflate, br | |
Referer: http://localhost/ | |
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 | |
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36 | |
Upgrade-Insecure-Requests: 1 | |
Connection: keep-alive | |
Host: localhost | |
Request body: |
+1 for
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
I simplified the script by removing all of the unnecessary object oriented overhead, and made the log file append.
https://gist.github.com/jaywilliams/bee2512f0f12d6791315d6939119e135
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My edit to dump request to server log via
php -S localhost:62000
.Output