Skip to content

Instantly share code, notes, and snippets.

@rossholmberg
Created April 12, 2018 21:55
Show Gist options
  • Select an option

  • Save rossholmberg/fc31e18e1e7f42fc068d9e0d382b4df5 to your computer and use it in GitHub Desktop.

Select an option

Save rossholmberg/fc31e18e1e7f42fc068d9e0d382b4df5 to your computer and use it in GitHub Desktop.
PHP to receive any POST request, and write it to file on the server.
<?php
/*
#### Tested using PHP 7 under Apache.
A script to capture a json POST request, and print it
(nearly*) as is to an output text file. Outputs are
written to a subfolder `outputs` (make sure this folder
exists), separated into one file per day.
This is intended for use in reverse-engineering POST
requests. Since it will accept and write anything, I
wouldn't recommend using it in production.
* We add a server-side timestamp to each entry.
2 options are provided, uncomment one option as needed.
OPTION 1 prints one line per request, in a json format.
eg:
{"2000-01-01 12:00:00":{"param1":"val1","param2","val2"}}
OPTION 2 prints one line per parameter, indented to group
each request. eg:
server.time: 2000-01-01 12:00:00:
param1: val1
param2: val2
*/
# parse server-side timestamps
$now = date( 'Y-m-d H:i:s' );
$today = date( 'Ymd' );
### OPTION 1 START ###
# specify the output file, separating by date
$file = "outputs/" . $today . ".js";
# construct a complete line for the output file
$string = '{"' . $now . '":' . json_encode( $_POST ) . "}\n";
# print the contructed line to the output file
file_put_contents( $file, $string, FILE_APPEND );
### OPTION 1 END ###
/*
### OPTION 2 START ###
# specify the output file, separating by date
$file = "outputs/" . $today . ".txt";
# write a timestamp line
file_put_contents( $file,
"server.time: $now:\n",
FILE_APPEND );
# write an indented line for each parameter
foreach ($_POST as $param_name => $param_val) {
file_put_contents( $file,
" $param_name: $param_val\n",
FILE_APPEND );
};
### OPTION 2 END ###
*/
# respond to the sender
header( "HTTP/1.1 200" );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment