public function actionApprovedfoodlist1() { $response = Yii::$app->response; $response->format = Response::FORMAT_JSON; $postdata = Yii::$app->request->post(); if (empty($postdata)) { $postdata = file_get_contents("php://input"); $postdata = json_decode($postdata, true); } // die('dfjsd0'); $foodNames = $postdata['food_names']; $appId = $this->nutrition_app_id; $appKey = $this->nutrition_app_key; $client = new ApprovedFood(); $url = 'https://api.nutritionix.com/v1_1/search'; // Set the query parameters for the API request $params = [ 'appId' => $appId, 'appKey' => $appKey, 'fields' => '*', 'brand_id' => '513fbc1283aa2dc80c00001d', 'query' => implode(',', $foodNames), ]; return $url . '?' . http_build_query($params); // Create a cURL request to the API endpoint $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); // Parse the JSON response and return it return json_decode($response, true); } public function actionApprovedfoodlist1() { $response = Yii::$app->response; $response->format = Response::FORMAT_JSON; $postdata = Yii::$app->request->post(); if (empty($postdata)) { $postdata = file_get_contents("php://input"); $postdata = json_decode($postdata, true); } // die('dfjsd0'); $foodNames = $postdata['food_names']; $appId = $this->nutrition_app_id; $appKey = $this->nutrition_app_key; $food_name ="Apple"; // $client = new ApprovedFood(); $url = 'https://api.nutritionix.com/v1_1/search/'.$food_name.'?results=0:1&fields=item_name,brand_name,item_id,nf_calories,nf_total_fat'; $url .= '&appId='.$appId.'&appKey='.$appKey; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $response = curl_exec($ch); // curl_exec($ch); if (curl_errno($ch)) { $error_msg = curl_error($ch); die('error'); return $error_msg; } curl_close($ch); $nutrition_data = json_decode($response, true); return $nutrition_data; // $calories = $nutrition_data['hits'][0]['fields']['nf_calories']; // $total_fat = $nutrition_data['hits'][0]['fields']['nf_total_fat']; }