Browse Source

Update Api Data Sock

docjyJ 1 year ago
parent
commit
88aada72b1
1 changed files with 17 additions and 27 deletions
  1. 17
    27
      classes/postHandler.php

+ 17
- 27
classes/postHandler.php View File

@@ -1,8 +1,5 @@
1 1
 <?php
2 2
 
3
-use OpenFoodFacts\Exception\BadRequestException;
4
-use OpenFoodFacts\Exception\ProductNotFoundException;
5
-
6 3
 require_once 'dao.php';
7 4
 
8 5
 class PostHandler
@@ -82,45 +79,29 @@ class PostHandler
82 79
 
83 80
     public function get_articles_json_list()
84 81
     {
85
-        $api = new OpenFoodFacts\Api('food','fr');
86 82
         $articles = $this->dao->get_articles();
87 83
         $formatted_articles = [];
88 84
         foreach ($articles as $article) {
89 85
             $article["type"] = $this->dao->get_categories_of_article($article["id"]);
90
-            $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
91 86
             //EXPERIMENTAL
92
-            try {
93
-                $product = $api->getProduct('3057640385148');
94
-                $article["image_v2"] = $product["image_front_url"];
87
+            $response = $this->get_openfoodfacts_product('3057640385148');
88
+            if($response["status"] == 1){
89
+                $product = $response["product"];
90
+                $article["image_"] = $product["image_front_url"] != "" ? $product["image_front_url"] : $this->imageBaseUrl . $article["id"] . ".jpg";;
95 91
                 $article["nutri_score"] = $product["nutrition_grade_fr"];
96 92
                 $article["ingredients_fr"] = $product["ingredients_text_fr"] != "" ? $product["ingredients_text_fr"] : $product["ingredients_text"];
97 93
                 $article["ingredients_en"] = $product["ingredients_text_en"] != "" ? $product["ingredients_text_en"] : $product["ingredients_text"];
98 94
                 $article["name_fr"] = $product["product_name_fr"] != "" ? $product["product_name_fr"] : $product["product_name"];
99
-                $article["name_fr"] = $product["product_name_en"] != "" ? $product["product_name_en"] : $product["product_name"];
100
-                $article["description_fr"] = $product["generic_name_fr"];
101
-                $article["description_en"] = $product["generic_name_en"];
95
+                $article["name_en"] = $product["product_name_en"] != "" ? $product["product_name_en"] : $product["product_name"];
102 96
                 $article["description_fr"] = $product["generic_name_fr"] != "" ? $product["generic_name_fr"] : $product["generic_name"];
103 97
                 $article["description_en"] = $product["generic_name_en"] != "" ? $product["generic_name_en"] : $product["generic_name"];
104
-            } catch (BadRequestException $e) {
105
-                $article["image_v2"] = "";
106
-                $article["nutri_score"] = "";
107
-                $article["ingredients_fr"] = "";
108
-                $article["ingredients_en"] = "";
109
-                $article["name_fr"] = "";
110
-                $article["name_fr"] = "";
111
-                $article["description_fr"] = "";
112
-                $article["description_en"] = "";
113
-                $article["description_fr"] = "";
114
-                $article["description_en"] = "";
115
-            } catch (ProductNotFoundException $e) {
116
-                $article["image_v2"] = "";
98
+            } else {
99
+                $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
117 100
                 $article["nutri_score"] = "";
118 101
                 $article["ingredients_fr"] = "";
119 102
                 $article["ingredients_en"] = "";
120 103
                 $article["name_fr"] = "";
121
-                $article["name_fr"] = "";
122
-                $article["description_fr"] = "";
123
-                $article["description_en"] = "";
104
+                $article["name_en"] = "";
124 105
                 $article["description_fr"] = "";
125 106
                 $article["description_en"] = "";
126 107
             }
@@ -130,6 +111,15 @@ class PostHandler
130 111
         return $formatted_articles;
131 112
     }
132 113
 
114
+    private function get_openfoodfacts_product($barcode){
115
+        $country = 'fr';
116
+        $productSlug = 'produit';
117
+        $url = 'https://{country}.openfoodfacts.org/api/v0/{product}/{scan}.json';
118
+        $url = str_replace(['{country}','{product}','{scan}'],[$country,$productSlug,$barcode],$url);
119
+        $result = file_get_contents($url);
120
+        return json_decode($result, true);
121
+    }
122
+
133 123
     private function save_image()
134 124
     {
135 125
         $success = true;

Loading…
Cancel
Save