Update Api Data Sock

This commit is contained in:
docjyJ 2020-08-23 13:01:19 +02:00
parent 384328739e
commit 88aada72b1

View file

@ -1,8 +1,5 @@
<?php
use OpenFoodFacts\Exception\BadRequestException;
use OpenFoodFacts\Exception\ProductNotFoundException;
require_once 'dao.php';
class PostHandler
@ -82,45 +79,29 @@ class PostHandler
public function get_articles_json_list()
{
$api = new OpenFoodFacts\Api('food','fr');
$articles = $this->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;