From 83cee48d363da4c276f8a94b50951a15e03eebd7 Mon Sep 17 00:00:00 2001 From: docjyJ Date: Sat, 29 Aug 2020 09:08:08 +0200 Subject: [PATCH] Update Code --- classes/postHandler.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/classes/postHandler.php b/classes/postHandler.php index 5ad7da7..4fe9f95 100644 --- a/classes/postHandler.php +++ b/classes/postHandler.php @@ -1,5 +1,4 @@ dao->get_categories_of_article($article["id"]); + $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg"; //EXPERIMENTAL - $response = $this->get_openfoodfacts_product($article["code"]); - $product = $response["product"]; - $article["image"] = $product["image_front_url"] != "" ? $product["image_front_url"] : $this->imageBaseUrl . $article["id"] . ".jpg"; - $article["nutri-score"] = !empty($product["nutrition_grade_fr"]) ? $product["nutrition_grade_fr"] : null; - $article["ingredients"] = !empty($product["ingredients_text_fr"]) ? $product["ingredients_text_fr"] : !empty($product["ingredients_text"]) ? $product["ingredients_text"] : null; - $article["name"] = !empty($product["product_name_fr"]) ? $product["product_name_fr"] : !empty($product["product_name"]) ? $product["product_name"] : $article["name"]; - $article["description"] = !empty($product["generic_name_fr"]) ? $product["generic_name_fr"] : !empty($product["generic_name"]) ? $product["generic_name"] : $article["description"]; + $product = $this->get_openfoodfacts_product($article["code"]); + if($product != null){ + $article["nutri-score"] = $product["nutrition_grade_fr"]; + if(!empty($product["ingredients_text_fr"])) $article["ingredients"] = $product["ingredients_text_fr"]; + else $article["ingredients"] = $product["ingredients_text"]; + if(!empty($product["generic_name_fr"])) $article["generic"] = $product["generic_name_fr"]; + else $article["generic"] = $product["generic_name"]; + } + else { + $article["nutri-score"] = ""; + $article["generic"] = ""; + $article["ingredients"] = ""; + } array_push($formatted_articles, $article); } @@ -102,8 +108,9 @@ class PostHandler $productSlug = 'produit'; $url = 'https://{country}.openfoodfacts.org/api/v0/{product}/{scan}.json'; $url = str_replace(['{country}','{product}','{scan}'],[$country,$productSlug,$barcode],$url); - $result = file_get_contents($url); - return json_decode($result, true); + $result = json_decode(file_get_contents($url), true); + if ($result["status"] == 1) return $result["product"]; + else return null; } private function save_image()