Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP script to dump full HTTP request to file (method, HTTP headers and body).
<?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');
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:
@beebopfr

This comment has been minimized.

Copy link

commented Oct 10, 2017

Hey ! Thanks for this helpfull script !
My suggestions :

  • remove 'echo("Done!\n\n");' : user of this script should be able to have control of what is written to the request output
  • change $data .= "\nResponse body:\n"; to $data .= "\nRequest body:\n"; ? Since what you dump is the body of the actual request, not the response
@magnetikonline

This comment has been minimized.

Copy link
Owner Author

commented Oct 20, 2017

@beebopfr Request body of course 👍 Thanks!

@chetanmadaan

This comment has been minimized.

Copy link

commented Dec 1, 2017

Hello,

It's a very useful script would be useful if it can dump
$_GET
data as well and also list
$_SERVER['REMOTE_ADDR']

@ToLive

This comment has been minimized.

Copy link

commented Dec 4, 2017

Nice script, thank you!

@justinDevel

This comment has been minimized.

Copy link

commented Feb 15, 2018

hi ,
am how to use this script to get the response ???

thanks in advance

@Kobily

This comment has been minimized.

Copy link

commented Feb 22, 2018

To add every new register instead of replacing the previous one, you must use FILE_APPEND with file_get_content.
$data . file_get_contents('php://input') . "\n*************************************************************\n", FILE_APPEND
Thank you.

@mustafaredsignal

This comment has been minimized.

Copy link

commented May 1, 2018

Use this line to get GET/POST data

$data . print_r($_REQUEST,true) . "\n*************************************************************\n", FILE_APPEND

@unionor

This comment has been minimized.

Copy link

commented May 14, 2018

add this line at the end to create a file for each request with timestamp

$date = new DateTime();
rename("dumprequest.txt", "dumprequest" . $date->format('Y-m-d H:i:sP') . ".txt");

@codebeat-nl

This comment has been minimized.

Copy link

commented Jul 28, 2018

This doesn't need to be a class, could be a easy to use library function.

@aago79

This comment has been minimized.

Copy link

commented Nov 23, 2018

Hey! thanks for this script.

I'm a complete newbie and maybe I can not even explain myself. However what I need is to save data coming from an http request to my server on a file. Thus it seems that the above script is perfect for my needs.
What I do not understand is where to put the script and how let it works: should I put the script on my server and use is URL in the http request to let him works and create the file?

thanks in advance

@jotham

This comment has been minimized.

Copy link

commented Mar 7, 2019

For the next time I come across this- Single function version.

function logRequest($targetFile){ $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,' ','-'); $headerList[$name] = $value; } } $data = sprintf("%s %s %s\n", $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']); foreach ($headerList as $name => $value) { $data .= $name.': '.$value."\n"; } $data .= "\n"; file_put_contents($targetFile, $data.file_get_contents('php://input')."\n"); }

logRequest("/tmp/post-".time().".log");

@beppe9000

This comment has been minimized.

Copy link

commented Jun 28, 2019

@jotham thanks for summarizing it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.