I am very much a JS person so some help with PHP would be appreciated :]
I have some data - JSON in the body of a POST request - which targets a PHP file. The php file needs to add each JSON object that comes in to an existing JSON array in a separate file.json file in the same folder. I have this currently:
<?php
$jsonString = file_get_contents("php://input");
$file = file_get_contents("testFile.json");
$fileData = json_decode($file);
$fileData[] = $jsonString;
$dataAsJson = json_encode($fileData);
file_put_contents($file, $dataAsJson);
echo '{ "success": true }';
?>
But started with this, which adds new JSON to the file but not in an array or with commas to separate each object:
<?php
$jsonString = file_get_contents("php://input");
$file = "testFile.json";
file_put_contents($file, $jsonString, FILE_APPEND);
echo '{ "success": true }';
?>
$jsonStringtoo before pushing it into$fileDataarray:$fileData[] = json_decode($jsonString);