I would like to get a part of a jsonfile content with a specific url with parameters. I have a results.json file contains the following:
{
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "2",
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011/07/25",
"office": "Tokyo",
"extn": "8422"
}
]
}
and I have a php file where i want to get and show a part of the json output from for exmaple id = 1 with the following link
/api.php?id=1
<?php
header("Content-Type:application/json");
function get_feed($getid)
{
$json = file_get_contents('results3.json');
$feeds = json_decode($json);
foreach($feeds as $feed=>$id)
{
if($id==$getid)
{
return $name;
break;
}
}
}
if(!empty($_GET['id']))
{
$getid=$_GET['id'];
$feed = get_feed($getid);
if(empty($feed))
{
response(200,"Data Not Found",NULL);
}
else
{
response(200,"Data Found",$feed);
}
}
else
{
response(400,"Invalid Request",NULL);
}
function response($status,$status_message,$data)
{
header("HTTP/1.1 ".$status);
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
but as result i get this when following the link /api.php?id=1
{"status":200,"status_message":"Data Not Found","data":null}
I properly don't go correctly through the array. Any help is appreciated! Thank you in advance!