diff --git a/classes/postHandler.php b/classes/postHandler.php index 581b2a0..022f193 100644 --- a/classes/postHandler.php +++ b/classes/postHandler.php @@ -1,8 +1,5 @@ dao->get_articles(); $formatted_articles = []; foreach ($articles as $article) { $article["type"] = $this->dao->get_categories_of_article($article["id"]); - $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg"; //EXPERIMENTAL - try { - $product = $api->getProduct('3057640385148'); - $article["image_v2"] = $product["image_front_url"]; + $response = $this->get_openfoodfacts_product('3057640385148'); + if($response["status"] == 1){ + $product = $response["product"]; + $article["image_"] = $product["image_front_url"] != "" ? $product["image_front_url"] : $this->imageBaseUrl . $article["id"] . ".jpg";; $article["nutri_score"] = $product["nutrition_grade_fr"]; $article["ingredients_fr"] = $product["ingredients_text_fr"] != "" ? $product["ingredients_text_fr"] : $product["ingredients_text"]; $article["ingredients_en"] = $product["ingredients_text_en"] != "" ? $product["ingredients_text_en"] : $product["ingredients_text"]; $article["name_fr"] = $product["product_name_fr"] != "" ? $product["product_name_fr"] : $product["product_name"]; - $article["name_fr"] = $product["product_name_en"] != "" ? $product["product_name_en"] : $product["product_name"]; - $article["description_fr"] = $product["generic_name_fr"]; - $article["description_en"] = $product["generic_name_en"]; + $article["name_en"] = $product["product_name_en"] != "" ? $product["product_name_en"] : $product["product_name"]; $article["description_fr"] = $product["generic_name_fr"] != "" ? $product["generic_name_fr"] : $product["generic_name"]; $article["description_en"] = $product["generic_name_en"] != "" ? $product["generic_name_en"] : $product["generic_name"]; - } catch (BadRequestException $e) { - $article["image_v2"] = ""; + } else { + $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg"; $article["nutri_score"] = ""; $article["ingredients_fr"] = ""; $article["ingredients_en"] = ""; $article["name_fr"] = ""; - $article["name_fr"] = ""; - $article["description_fr"] = ""; - $article["description_en"] = ""; - $article["description_fr"] = ""; - $article["description_en"] = ""; - } catch (ProductNotFoundException $e) { - $article["image_v2"] = ""; - $article["nutri_score"] = ""; - $article["ingredients_fr"] = ""; - $article["ingredients_en"] = ""; - $article["name_fr"] = ""; - $article["name_fr"] = ""; - $article["description_fr"] = ""; - $article["description_en"] = ""; + $article["name_en"] = ""; $article["description_fr"] = ""; $article["description_en"] = ""; } @@ -130,6 +111,15 @@ class PostHandler return $formatted_articles; } + private function get_openfoodfacts_product($barcode){ + $country = 'fr'; + $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); + } + private function save_image() { $success = true;