Skip to content

Instantly share code, notes, and snippets.

@alegut
Created December 26, 2018 09:25
Show Gist options
  • Save alegut/00944b9b3e7ee897cbdcb696e6a44941 to your computer and use it in GitHub Desktop.
Save alegut/00944b9b3e7ee897cbdcb696e6a44941 to your computer and use it in GitHub Desktop.
Send JSON data from POST request to txt file
<?php
header("Content-Type: application/json; charset=UTF-8");
$data = json_decode(file_get_contents('php://input'), true);
//print_r($data);
foreach($data as $key => $value)
{
$str.=$key.":".$value."\n";
}
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "new Record: \n".$str;
// Write the contents back to the file
file_put_contents($file, $current);
echo $str;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment