Skip to content

Instantly share code, notes, and snippets.

@alevgenk
Forked from magnetikonline/dumprequest.php
Last active November 17, 2021 11:51
Show Gist options
  • Save alevgenk/2fd826b6435521e154a44d04e0b4d7ea to your computer and use it in GitHub Desktop.
Save alevgenk/2fd826b6435521e154a44d04e0b4d7ea to your computer and use it in GitHub Desktop.
PHP script to dump full HTTP request to file (method, HTTP headers and body).
<?php
// Original from https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
class DumpHTTPRequestToFile
{
public function execute($targetFile, $append = true): void
{
$data = sprintf(
"%s\n%s %s %s\n\nHTTP headers:\n",
date("Y-m-d H:i:s"),
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI'],
$_SERVER['SERVER_PROTOCOL']
);
foreach ($this->getHeaderList() as $name => $value) {
$data .= $name . ': ' . $value . "\n";
}
$data .= "\nRequest body:\n";
$data .= file_get_contents('php://input') . "\n\n";
$data .= "======================================================================" . "\n\n";
$flag = $append ? FILE_APPEND : null;
file_put_contents($targetFile, $data, $flag);
echo("Done!\n\n");
}
private function getHeaderList(): array
{
$headerList = [];
foreach ($_SERVER as $name => $value) {
if (0 === strpos($name, "HTTP_")) {
// convert HTTP_HEADER_NAME to Header-Name
$name = str_replace('_', ' ', substr($name, 5));
$name = ucwords(strtolower($name));
$name = str_replace(' ', '-', $name);
// add to list
$headerList[$name] = $value;
}
}
return $headerList;
}
}
(new DumpHTTPRequestToFile)->execute('./dumprequest.txt');
2021-11-17 14:49:12
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:
{"arg":123}
======================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment