private function update_weather($list, $town_id) { for ($i = 0; $i < sizeof($list); $i++) { $date = date("Y-m-d H:i:s", $list[$i]['dt']); $temp_min = $list[$i]['main']['temp_min']; $temp_max = $list[$i]['main']['temp_max']; $temp_min = round($temp_min - 273.15); $temp_max = round($temp_max - 273.15); $weather_icon = $list[$i]['weather'][0]['icon']; $wind_icon = $list[$i]['wind']['deg']; $weather = new Weather(); $weather->town_id = $town_id; $weather->temp_min = $temp_min; $weather->temp_max = $temp_max; $weather->kuupaev = $date; $weather->weather_icon = $weather_icon; $weather->wind_icon = $wind_icon; $weather->save(); } }
/** * Display a listing of the resource. * * @return Response */ public function getWeather() { $key = env('FORECAST_IO_API_KEY'); $forecast = new Forecast($key); $weather = $forecast->get('34.101655', '-117.707591'); for ($i = 0; $i < 7; $i++) { if ($i == 0) { $current = $weather->currently->temperature; } else { $current = -1; } $date = Carbon::createFromTimeStamp($weather->daily->data[$i]->time)->toDateTimeString(); $dateId = substr($date, 0, 10); $id = DB::table('email_articles')->where('post_date', $dateId)->value('article_id'); $icon = $weather->daily->data[$i]->icon; if (strpos($icon, 'night') !== false) { $icon = 'clear-day'; } $max = $weather->daily->data[$i]->temperatureMax; $min = $weather->daily->data[$i]->temperatureMin; $sunset = Carbon::createFromTimeStamp($weather->daily->data[$i]->sunsetTime)->toDateTimeString(); $sunrise = Carbon::createFromTimeStamp($weather->daily->data[$i]->sunriseTime)->toDateTimeString(); echo strpos($icon, 'night'); if (Weather::where('article_id', '=', $id)->exists()) { DB::table('weather')->where('article_id', $id)->update(array('icon' => $icon, 'current_temp' => $current, 'max' => $max, 'min' => $min, 'sunriseTime' => $sunrise, 'sunsetTime' => $sunset, 'updated_at' => Carbon::now())); echo "Updated " . $date; } else { $entry = new Weather(); $entry->article_id = $id; $entry->icon = $icon; $entry->current_temp = $current; $entry->max = $max; $entry->min = $min; $entry->sunsetTime = $sunset; $entry->sunriseTime = $sunrise; $entry->save(); echo "Stored " . $date . "!"; } } }
public function refresh($town) { $town_id = DB::table('towns')->where('town', $town)->first(); $town_id = $town_id->id; //получить id города $response = json_decode(file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=' . $town . '&mode=json&appid=f84ba1064b0ae65792326548686f361c'), true); // print_r($response); $id = $response['city']['id']; $date_today = date("Y-m-d", strtotime("+0 day")); DB::table('weather')->where('kuupaev', '>=', $date_today)->where('town_id', $town_id)->delete(); for ($i = 0; $i < sizeof($response['list']); $i++) { $date = date("Y-m-d H:i:s", $response['list'][$i]['dt']); $temp_min = $response['list'][$i]['main']['temp_min']; $temp_max = $response['list'][$i]['main']['temp_max']; $temp_min = round($temp_min - 273.15); $temp_max = round($temp_max - 273.15); $weather = new Weather(); $weather->town_id = $id; $weather->temp_min = $temp_min; $weather->temp_max = $temp_max; $weather->kuupaev = $date; $weather->save(); } return redirect('weather/' . $town); }