Whilst storing data in a json file would not be preferred over using a MYSQL database here is how to do it with a quick example and then a more advanced use:
$id = 5671;
$fetch = json_decode(file_get_contents("https://apiurl.com&id=".$id.""),true);
$name = $fetch->name;
$age = $fetch->age;
$data = array('id' => $id, 'name' => $name, 'age' => $age);
$fp = fopen("".$id.".json", "w");//saves .json file as 5671.json
fwrite($fp, $data);
fclose($fp);More advanced:
profile.php?id=5671
$id = $_GET['id'];
function save_data($id) {
$fetch = json_decode(file_get_contents("https://apiurl.com&id=".$id.""),true);
$name = $fetch->name;
$age = $fetch->age;
$data = array('id' => $id, 'name' => $name, 'age' => $age);
$fp = fopen("".$id.".json", "w");//saves .json file as 5671.json
fwrite($fp, $data);
fclose($fp);
return json_encode($data);
}
echo save_data($id);and to read saved json file:
read.php?id=5671
$id = $_GET['id'];
function get_data($id) {
if (file_exists("".$id.".json")) {
$fetch = json_decode(file_get_contents("https://mydomain.com/".$id.".json"),true);
$name = $fetch->name;
$age = $fetch->age;
return array('id' => $id, 'name' => $name, 'age' => $age);
} else {
return "Json file for ".$id." does not exist";
}
}
echo get_data($id);