コード例 #1
0
 public function retrieve(Request $request, Weather $weather)
 {
     if (!$request->has('lat') || !$request->has('lon')) {
         return response('Please provide a lat and lon', 400);
     }
     $input = $request->all();
     // We grab the lat and lon
     $lat = $input['lat'];
     $lon = $input['lon'];
     // We grab the latest data from this lat and long
     $rawData = file_get_contents('http://api.openweathermap.org/data/2.5/weather?lat=' . $lat . '&lon=' . $lon);
     if (!$rawData) {
         // We failed to retrieve data from the webservice
         // Just return the stuff we have
         return $weather->where('lat', $lat)->where('lon', $lon)->limit(10)->get();
     }
     $jsonData = json_decode($rawData, true);
     // We transform this data
     $data = ['dt' => $jsonData['dt'], 'lat' => $lat, 'lon' => $lon, 'type' => $jsonData['weather'][0]['main'], 'temp' => $jsonData['main']['temp'] - 273.15];
     // Check if we already have a record with same lat/lon and dt in our database
     $weatherCheck = $weather->where('lat', $lat)->where('lon', $lon)->where('dt', Carbon::createFromTimeStamp($data['dt']))->limit(1)->get();
     // Record isn't in our db yet
     if ($weatherCheck->isEmpty()) {
         // We store the data in our database
         $weather->create($data);
     }
     // We grab the last 10 weather report from given lat/lon and return it
     return $weather->where('lat', $lat)->where('lon', $lon)->limit(10)->get();
 }
コード例 #2
0
 public function cronWeather()
 {
     Weather::truncate();
     $hoteluri = Hoteluri::all();
     foreach ($hoteluri as $hotel) {
         $file = "http://api.yr.no/weatherapi/locationforecast/1.9/?lat={$hotel->Latitude};lon={$hotel->Longitude};msl=70";
         $xml = new SimpleXMLElement($file, null, TRUE);
         $temp = $xml->product->time;
         foreach ($temp as $xml_time) {
             foreach ($xml_time->location as $xml_loc) {
                 if ($xml_loc->minTemperature && $xml_loc->maxTemperature && $xml_loc->symbol) {
                     $mintemp = $xml_loc->minTemperature->attributes()->value;
                     $maxtemp = $xml_loc->maxTemperature->attributes()->value;
                     $array_min = array_filter(preg_split("(T|Z)", $xml_time->attributes()->from)) + ['2' => (string) $mintemp];
                     $array_max = array_filter(preg_split("(T|Z)", $xml_time->attributes()->to)) + ['2' => (string) $maxtemp];
                     $icon = "http://api.yr.no/weatherapi/weathericon/1.1/?symbol=" . $xml_loc->symbol->attributes()->number . ";is_night=1;content_type=image/png";
                     $array = ['HotelID' => $hotel->id, 'FromDate' => $array_min[0], 'FromHour' => $array_min[1], 'MinDegrees' => $array_min[2], 'ToDate' => $array_max[0], 'ToHour' => $array_max[1], 'MaxDegrees' => $array_max[2], 'Logo' => $xml_loc->symbol->attributes()->number];
                     Weather::create($array);
                 }
             }
         }
     }
 }