4
I have a JSON file in the following template:
[
{
"id": 1
"nome": "José"
},
{
"id": 2
"nome": "João"
}
]
And I’m making the following JSON server in PHP so that I get the entire JSON back. The way my code is it just returns each position 1 at a time
$path = explode('/', $_GET['path']);
$contents = file_get_contents('tickets.json');
$json = json_decode($contents, true);
$method = $_SERVER['REQUEST_METHOD'];
header('Content-type: application/json');
$body = file_get_contents('php://input');
if ($method === 'GET') {
if ($json[$path[0]]) {
echo json_encode($json[$path[0]]);
}else{
echo '[]';
}
}
When I am the ?path=0
it returns me to position 0 with id 1 I wanted it to return everything inside the json if I put nothing in the path and if I put the ID in the path it returns me only 1
You want it to return all the values that are in the JSON file when you receive the value 0 in GET?
– Ismael SIlva
@Ismaelsilva I want that if I do not put anything it returns me all and if I put something in the example path the ID and it returns me only one
– user3140824
Your path variable, will it exist even without value? Example, "? path="
– Ismael SIlva
@Ismaelsilva Not necessarily, I just want a getAll if it doesn’t have a path or if it has a getById path
– user3140824