Skip to content

Instantly share code, notes, and snippets.

@vigikaran
Last active August 16, 2022 09:30
Show Gist options
  • Save vigikaran/d068cc79dc78f8295388d657ebeaf4ef to your computer and use it in GitHub Desktop.
Save vigikaran/d068cc79dc78f8295388d657ebeaf4ef to your computer and use it in GitHub Desktop.
<?php
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",
FILE_APPEND | LOCK_EX
);
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;
}
}
$headerList['ip'] = isset($_SERVER['HTTP_CLIENT_IP'])
? $_SERVER['HTTP_CLIENT_IP']
: (isset($_SERVER['HTTP_X_FORWARDED_FOR'])
? $_SERVER['HTTP_X_FORWARDED_FOR']
: $_SERVER['REMOTE_ADDR']);
return $headerList;
}
}
(new DumpHTTPRequestToFile)->execute('./dumprequest.txt');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment