Skip to content

Instantly share code, notes, and snippets.

@sdennler
Forked from magnetikonline/dumprequest.php
Last active August 18, 2017 15:12
Show Gist options
  • Save sdennler/9845adc3967b3328177ac86edb1c98ba to your computer and use it in GitHub Desktop.
Save sdennler/9845adc3967b3328177ac86edb1c98ba to your computer and use it in GitHub Desktop.
PHP script to dump full HTTP request to file (method/HTTP headers and request body).
<?php
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
class DumpHTTPRequest {
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";
}
file_put_contents(
$targetFile,
$data . "\nResponse body:\n" . file_get_contents('php://input') . "\n"
);
header('Content-Type: text/plain');
echo("Done!\n\n" . $data);
}
private function getHeaderList() {
$headerList = array();
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;
}
}
$d = new DumpHTTPRequest;
$d->execute('./dumprequest.txt');
GET /dumprequest.php HTTP/1.1
HTTP headers:
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36
Referer: http://localhost/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Response body:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment