Skip to content

Instantly share code, notes, and snippets.

@dzungtran
Last active August 1, 2019 08:03
Show Gist options
  • Save dzungtran/880e416db393042536d1951a028ab00a to your computer and use it in GitHub Desktop.
Save dzungtran/880e416db393042536d1951a028ab00a to your computer and use it in GitHub Desktop.
Create request logging server on local

Create request logging server on local (for testing purpose)

  • Create index.php
  • Start php built-in server on local by command: php -S localhost:8000
  • Publish local server to serveo: ssh -o ServerAliveInterval=60 -R 80:localhost:8000 serveo.net
<?php
class DumpHTTPRequestToFile {
public function execute($targetFile) {
$data = sprintf(
"%s %s %s\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";
if (isset($_SERVER["CONTENT_TYPE"]) && $_SERVER["CONTENT_TYPE"] == "multipart/form-data") {
$data .= print_r($_POST, true);
} else {
$data .= file_get_contents('php://input');
}
file_put_contents(
$targetFile,
"=============== Request time: " . date(DATE_RFC2822) . " ==================\n\n". $data . "\n\n", FILE_APPEND
);
echo("OK");
}
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('./files/' . date('Ymd') . '.txt');
cd ~/your_folder & php -S localhost:6969 &> /dev/null &
ssh -o ServerAliveInterval=60 -R your_sub_domain:80:localhost:6969 serveo.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment