So I'm doing a project for college involving an API for information about food, it returns the food brand, id, energy and fat etc.. via JSON, it also returns lots of other information that I don't want via JSON so I'm wondering how I can be selective with what I return and then use.
I'm connecting to the API using PHP and this is the code I'm using.
<?php
//$ingr = "ingr=" . $_GET['foodName'];
$ingr = "ingr=Bread";
//echo getFoodFromAPI($ingr);
getFoodFromAPI($ingr);
//return $decodedData;
function getFoodFromAPI($ingr){
/*$queryParams = array(
'Food' => $JsonFood
);*/
//echo $JsonFood;
$api = "https://api.edamam.com/api/food-database/parser?";
$apiId = "&app_id=ABC";
$apiKey = "&app_key=ABC";
$url = $api . $ingr . $apiId . $apiKey; //+ $search
$JsonContents = file_get_contents($url);
$Json = json_decode($JsonContents);
//echo $JsonBrand."Hello There Brand";
for ($x = 1; $x <= 10; $x++) {
$JsonBrand = $Json->hints[$x]->{'food'}->{'brand'} . "<br>"; //used to echo the values of the decoded data
$JsonLabel = $Json->hints[$x]->{'food'}->{'label'} . "<br>"; //used to echo the values of the decoded data
echo $JsonBrand;
echo $JsonLabel;
}
$JsonRec = json_encode($Json);
//print_r($JsonRec->hints->food);
return $JsonRec;
};
?>
So my question is this, is there a way to return only certain parts of the JSON decode? I don't need/want certain information (eg: Return only the brand & id of the food) and also is there a way I can use the returned data being either a PHP object OR array in javascript (i.e show it to a client on a webpage)
Please feel free to ask me to clarify anything I've failed to explain, like I said I'm a college student so I'm still learning and grasping the languages as I use them.
Also for reference here is the HTML & JavaScript I was working with and again I don't know if this is correct either because there is a mix between AJAX and JQuery.
function find_food(foodName) {
var vars = "foodName" + foodName;
var result = "";
$.ajax({
url: "sendFood.php",
type: "GET",
data: vars,
async: false,
success: function(data) {
console.log(data + "Data Here !");
result = data;
}
});
return result;
//console.log(result);
//return result;
}
function searchFoodDic() {
var food = document.getElementById("foodName").value;
console.log(food);
//var htmlText = "";
document.getElementById("searchedFood").innerHTML = "processing...";
var foodSearched = find_food(food);
console.log('>>>> searched food: ', foodSearched);
//htmlText += '<p class="p-name"> Train Code: ' + foodSearched[1].brand + '</p>';
//var filteredFood = getCommonFoods(foodSearched.food);
//console.log('>>>> filtered food: ', filteredFood);
htmlText = "Hello";
for (i = 0; i < 5; i++) {
//htmlText += '<p class="p-foodName"> Food Name: ' + data.hints[i].food.brand + '</p>';
htmlText += '<p class="p-foodLabel1"> Food Label 1: ' + foodSearched.foodId + '</p>';
htmlText += '<p class="p-foodLabel2"> Food Label 2: ' + foodSearched.label + '</p>';
htmlText += '<br/>';
}
document.getElementById("searchedFood").innerHTML = htmlText;
}
<link rel="stylesheet" type="text/css" href="food.css">
<html>
<head>
<ul>
<li><a class="active" href="index.php">Home </a></li>
<li><a href="admin.html">Administration</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<title> Food Website </title>
<body>
<script src="searchFood.js" type="text/javascript"></script>
<div class="food-container">
<input type="text" id="foodName" name="foodName" placeholder="Enter food eg: Apple, Bread">
<input type="submit" onclick="searchFoodDic()">
<div id="searchedFood">
</div>
</div>
</body>
</html>