My code so far :
<?PHP
header('Access-Control-Allow-Origin: *');
$src = $_POST["src"];
$jsonString = file_get_contents('jsonFile.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['src'] == $src) {
$data[$key]['counter']++;
}
}
$newJsonString = json_encode($data);
file_put_contents('jsonFile.json', $newJsonString);
?>
My json file looks like this:
[
{
"src": "",
"counter": "0"
},
{
"src": "",
"counter": "0"
} // and so on...
Expected behaviour is to increment the counter value of matching source supplied by POST by 1.
The error in logs is PHP Warning: Invalid argument supplied for foreach() in /file.php on line 9 .
When I do var_dump($data)
I get output NULL
.
Where do I go wrong ?