Difference between revisions of "Receive JSON POST Data"
Jump to navigation
Jump to search
Michael.mast (talk | contribs) |
Michael.mast (talk | contribs) |
||
| Line 15: | Line 15: | ||
</pre> | </pre> | ||
To further build on this, and to keep track of the received hooks, I wanted to add a date/time stamp to the file<ref>http://php.net/manual/en/function.date.php</ref>. | To further build on this, and to keep track of the received hooks, I wanted to add a date/time stamp to the file<ref>http://php.net/manual/en/function.date.php</ref>. | ||
| + | <pre> | ||
| + | <?php | ||
| + | $today = date("mdY_His"); | ||
| + | $myFile = "testFile_$today.txt"; | ||
| + | $data = file_get_contents('php://input'); | ||
| + | echo $data; | ||
| + | file_put_contents($myFile,$data); | ||
| + | echo date("Y"); | ||
| + | ?> | ||
| + | </pre> | ||
Revision as of 10:17, 26 July 2018
Purpose
Request came across my desk for a web server to receive JSON POST data, providing web hooks for a business analyst to parse. On my end things were simple enough.
- Simple Apache server to receive requests.
- Basic Authentication
- Save the POST data to file (For now, but I feel this will be expanded in the near future)
Process
[1][2]This example will echo the posted data back to the client during testing, as well as save the file to disk.
<?php
$myFile = "testFile.txt";
$data = file_get_contents('php://input');
echo $data;
file_put_contents($myFile,$data);
?>
To further build on this, and to keep track of the received hooks, I wanted to add a date/time stamp to the file[3].
<?php
$today = date("mdY_His");
$myFile = "testFile_$today.txt";
$data = file_get_contents('php://input');
echo $data;
file_put_contents($myFile,$data);
echo date("Y");
?>