Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active August 27, 2024 15:59
Show Gist options
  • Save magnetikonline/650e30e485c0f91f2f40 to your computer and use it in GitHub Desktop.
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).
<?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
Copy link

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
Copy link
Author

@beebopfr Request body of course 👍 Thanks!

@chetanmadaan
Copy link

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
Copy link

ToLive commented Dec 4, 2017

Nice script, thank you!

@justinDevel
Copy link

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

thanks in advance

@Kobily
Copy link

Kobily 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
Copy link

Use this line to get GET/POST data

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

@unionor
Copy link

unionor 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
Copy link

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

@aago79
Copy link

aago79 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
Copy link

jotham 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
Copy link

beppe9000 commented Jun 28, 2019

@jotham thanks for summarizing it

@mabrahamde
Copy link

Simple, but useful snippet. :)

@G0ldmember
Copy link

Yeah I also find that FILE_APPEND would be great so you can tail -f the log file and it does not get overwritten all the time. Apart from that, useful PHP snippet ;). THX

@message
Copy link

message commented Dec 15, 2021

My edit to dump request to server log via php -S localhost:62000.

<?php

$time = time();
error_log("--------------------[START/" . $time . "]--------------------");
error_log("");
error_log(sprintf("%s %s %s", $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']));
error_log("");
error_log("");
foreach ($_SERVER as $headerName => $headerValue) {
	if (preg_match('/^HTTP_/', $headerName)) {
		// convert HTTP_HEADER_NAME to Header-Name
		$headerName = strtr(substr($headerName, 5), '_', ' ');
		$headerName = ucwords(strtolower($headerName));
		$headerName = strtr($headerName, ' ', '-');

		error_log("$headerName: $headerValue");
	}
}
error_log("");
$body = file_get_contents('php://input');
$bodyLength = strlen($body);
error_log("");
error_log($bodyLength ? $body : "(empty body)");
error_log("");
error_log("---------------------[END/" . $time . "]---------------------");

Output

[Wed Dec 15 13:45:00 2021] 127.0.0.1:40922 Accepted
[Wed Dec 15 13:45:00 2021] --------------------[START/1639568700]--------------------
[Wed Dec 15 13:45:00 2021] 
[Wed Dec 15 13:45:00 2021] POST / HTTP/1.0
[Wed Dec 15 13:45:00 2021] 
[Wed Dec 15 13:45:00 2021] 
[Wed Dec 15 13:45:00 2021] X-Real-Ip: 127.0.0.1
[Wed Dec 15 13:45:00 2021] X-Forwarded-For: 127.0.0.1
[Wed Dec 15 13:45:00 2021] Host: 127.0.0.1
[Wed Dec 15 13:45:00 2021] Connection: close
[Wed Dec 15 13:45:00 2021] Content-Length: 11
[Wed Dec 15 13:45:00 2021] User-Agent: HTTPie/1.0.3
[Wed Dec 15 13:45:00 2021] Accept-Encoding: gzip, deflate
[Wed Dec 15 13:45:00 2021] Accept: */*
[Wed Dec 15 13:45:00 2021] Content-Type: application/x-www-form-urlencoded; charset=utf-8
[Wed Dec 15 13:45:00 2021] 
[Wed Dec 15 13:45:00 2021] 
[Wed Dec 15 13:45:00 2021] hello=world
[Wed Dec 15 13:45:00 2021] 
[Wed Dec 15 13:45:00 2021] ---------------------[END/1639568700]---------------------
[Wed Dec 15 13:45:00 2021] 127.0.0.1:40922 [200]: POST /
[Wed Dec 15 13:45:00 2021] 127.0.0.1:40922 Closing

@delfer
Copy link

delfer commented Mar 30, 2022

+1 for
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);

@jaywilliams
Copy link

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