private function weatherAction($lat, $long, $date, $hour) { $data = array(); $data['date'] = $date; $data['hour'] = $hour; $data['lat'] = $lat; $data['lon'] = $long; $owm = new OpenWeatherMap(); $units = $this->container->getParameter('units'); $lang = $this->container->getParameter('lang'); $api_key = $this->container->getParameter('api_key'); $dat = new \DateTime($date); $array_lat_lon = array(); $array_lat_lon['lat'] = $lat; $array_lat_lon['lon'] = $long; try { $weather = $owm->getWeather($array_lat_lon, $units, $lang, $api_key); // $weather = $owm->getWeatherHistory($array_lat_lon, $dat, 1, $type = 'hour', $units, $lang, $api_key); } catch (OWMException $e) { echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; echo "<br />\n"; } catch (\Exception $e) { echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; echo "<br />\n"; } $data['weather'] = $weather->weather->description; return $data; }
/** * display the admin dashboard */ public function index() { $adminMenu = Menu::get('admin_sidebar'); $adminMenu->setActiveMenu('dashboard'); $user = Sentry::getUser(); $this->data['usercity'] = DB::table('user_to_city')->join('weather_city', 'user_to_city.id_city', '=', 'weather_city.id')->select('weather_city.name_city')->where('id_user', $user['id'])->get(); // Language of data (try your own language here!): $lang = 'fr'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). $dataweather = array(); foreach ($this->data['usercity'] as $namecity) { //print_r($namecity['name_city']); $owm = new OpenWeatherMap(); try { $weather = $owm->getWeather($namecity['name_city'], $units, $lang); $dataweather[] = $weather; $this->data['dataweather'] = $dataweather; } catch (OWMException $e) { echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; echo "<br />\n"; } catch (\Exception $e) { echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; echo "<br />\n"; } } //print_r($this->data['dataweather']); View::display('admin/index.twig', $this->data); }
public function testUnauthorizedAccess() { try { $this->owm->getWeather('Paris'); } catch (OWMException $e) { $this->assertEquals(401, $e->getCode()); $this->assertRegExp('/^Invalid API key/', $e->getMessage()); } }
public function pushOpenWeatherData($db) { $owm = new OpenWeatherMap(); $weather = $owm->getWeather($this->getApplication()->getSilexApplication()['config']['openweather']['city_id'], 'metric', 'en'); $temperature = $weather->temperature->now->getValue(); $humidity = $weather->humidity->getValue(); $pressure = $weather->pressure->getValue(); $wind = $weather->wind->speed->getValue(); $clouds = $weather->clouds->getValue(); $precipitation = $weather->precipitation->getValue(); $db->insert("outside.temperature", ['fields' => array('value' => $temperature)]); $db->insert("outside.humidity", ['fields' => array('value' => $humidity)]); $db->insert("outside.pressure", ['fields' => array('value' => $pressure)]); $db->insert("outside.wind", ['fields' => array('value' => $wind)]); $db->insert("outside.clouds", ['fields' => array('value' => $clouds)]); $db->insert("outside.precipitation", ['fields' => array('value' => $precipitation)]); }
/** *For showing weather report */ public function getWeatherReport() { // Language of data (try your own language here!): $lang = 'en'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; $cityName = Input::get("selectedCityName"); $owm = new OpenWeatherMap(); //To get the current weather by passing arguments city name , unit , language and open map api key $weather = $owm->getWeather($cityName, $units, $lang, "125a338d7e70a75c62592fe7efed8060"); $returnData['selectedCity'] = $cityName; $returnData['now'] = $weather->temperature->now->getValue(); $returnData['min'] = $weather->temperature->min->getValue(); $returnData['max'] = $weather->temperature->max->getValue(); $returnData['unit'] = $weather->temperature->max->getUnit(); return view("home.ShowWeatherReport", compact("returnData")); }
/** * @param Application $app An Application instance * * @return ControllerCollection A ControllerCollection instance */ public function connect(Application $app) { $this->initTwig(__DIR__ . '/views'); $controllers = $app['controllers_factory']; $controllers->get('/', function (Application $app) { $request = $app['request']; $city = $request->get('city'); if (empty($city)) { return $this->twig->render('error.html.twig'); } $appId = $request->get('appId', ''); $openWeatherMap = new OpenWeatherMap(); try { $weather = $openWeatherMap->getWeather($city, 'metric', 'en', $appId); } catch (\Exception $e) { return $this->twig->render('error.html.twig'); } return $this->twig->render('weather.html.twig', ['id' => $request->get('id'), 'temperature' => (int) $weather->temperature->now->getValue(), 'icon' => $this->codeToIcon($weather->weather->icon), 'city' => $weather->city->name]); }); return $controllers; }
public function testWindMetric() { $forecast = $this->owm->getWeatherForecast('Moscow', 'metric', 'ru', '', 7); $this->assertEquals('Moscow', $forecast->city->name); $this->assertEquals('RU', $forecast->city->country); $this->assertEquals(524901, $forecast->city->id); $this->assertEquals('37.615555', $forecast->city->lon); $this->assertEquals('55.75222', $forecast->city->lat); $this->assertEquals('03:22:56', $forecast->sun->rise->format("H:i:s")); $this->assertEquals('15:50:08', $forecast->sun->set->format("H:i:s")); $this->assertEquals(7, iterator_count($forecast)); $forecast_arr = iterator_to_array($forecast); $this->assertEquals('5.41 m/s', $forecast_arr[0]->wind->speed); $this->assertEquals('61 ENE', $forecast_arr[1]->wind->direction); }
*/ use Cmfcmf\OpenWeatherMap; if (file_exists('../vendor/autoload.php')) { // Library is not part of a project. "composer install" was executed directly on this library's composer file. require '../vendor/autoload.php'; } else { // Library is part of a project. /** @noinspection PhpIncludeInspection */ require '../../../autoload.php'; } // Language of data (try your own language here!): $lang = 'de'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). $owm = new OpenWeatherMap(); // Example 1: Get forecast for the next 10 days for Berlin. $forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 10); echo "EXAMPLE 1<hr />\n\n\n"; echo "City: " . $forecast->city->name; echo "<br />\n"; echo "LastUpdate: " . $forecast->lastUpdate->format('d.m.Y H:i'); echo "<br />\n"; echo "<br />\n"; foreach ($forecast as $weather) { // Each $weather contains a Cmfcmf\ForecastWeather object which is almost the same as the Cmfcmf\Weather object. // Take a look into 'Examples_Current.php' to see the available options. echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i'); echo "<br />\n"; echo $weather->temperature; echo "<br />\n";
use Cmfcmf\OpenWeatherMap; use Cmfcmf\OpenWeatherMap\Exception as OWMException; $app->get('/', function () use($app) { $view = []; // NEWS SLIDE Feed::$cacheExpire = "30 minutes"; Feed::$cacheDir = "/tmp"; $rss_npr = Feed::loadRss("http://www.npr.org/rss/rss.php?id=1001"); $rss_techmeme = Feed::loadRss("http://www.techmeme.com/feed.xml"); $view['news_npr'] = $rss_npr->item; $view['news_techmeme'] = $rss_techmeme->item; // WEATHER SLIDE $lang = 'en'; $units = 'imperial'; $owm = new OpenWeatherMap(null, new WeatherCache(), 60); try { $weather = $owm->getWeather('Milpitas', $units, $lang, "06b7f9678c0512b3b8ca9e94758a9601"); $rawForecast = $owm->getWeatherForecast("Milpitas", $units, $lang, "06b7f9678c0512b3b8ca9e94758a9601", 3); $forecast = []; while ($rawForecast->valid()) { $chunk = $rawForecast->current(); $forecast[] = ['start' => $chunk->time->from->getTimestamp() - 3600 * 8, 'low' => $chunk->temperature->min->getValue(), 'avg' => $chunk->temperature->now->getValue(), 'max' => $chunk->temperature->max->getValue(), 'pressure' => $chunk->pressure->getValue(), 'precipitation' => $chunk->precipitation->getValue(), 'humidity' => $chunk->humidity->getValue(), 'clouds' => $chunk->clouds->getValue()]; $rawForecast->next(); } $forecastAvg = []; $forecastLow = []; $forecastHigh = []; $forecastPrecip = []; $fp = 0.5; foreach ($forecast as $f) {
use Cmfcmf\OpenWeatherMap; use Cmfcmf\OpenWeatherMap\Exception as OWMException; if (file_exists('../vendor/autoload.php')) { // Library is not part of a project. "composer install" was executed directly on this library's composer file. require '../vendor/autoload.php'; } else { // Library is part of a project. /** @noinspection PhpIncludeInspection */ require '../../../autoload.php'; } // Language of data (try your own language here!): $lang = 'de'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). $owm = new OpenWeatherMap(); // Example 1: Get current temperature in Berlin. $weather = $owm->getWeather('Berlin', $units, $lang); echo "EXAMPLE 1<hr />\n\n\n"; // $weather contains all available weather information for Berlin. // Let's get the temperature: // Returns it as formatted string (using __toString()): echo $weather->temperature; echo "<br />\n"; // Returns it as formatted string (using a method): echo $weather->temperature->getFormatted(); echo "<br />\n"; // Returns the value only: echo $weather->temperature->getValue(); echo "<br />\n"; // Returns the unit only:
if (!file_exists($path) || filemtime($path) + $this->seconds < time()) { echo "Weather data is NOT cached!\n"; return false; } echo "Weather data is cached!\n"; return true; } /** * @inheritdoc */ public function getCached($url) { return file_get_contents($this->urlToPath($url)); } /** * @inheritdoc */ public function setCached($url, $content) { file_put_contents($this->urlToPath($url), $content); } } // Language of data (try your own language here!): $lang = 'de'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; // Example 1: Use your own cache implementation. Cache for 10 seconds only in this example. $owm = new OpenWeatherMap(null, new ExampleCache(), 10); $weather = $owm->getWeather('Berlin', $units, $lang); echo "EXAMPLE 1<hr />\n\n\n"; echo $weather->temperature;
public function testByCoordinates() { $weather = $this->owm->getWeather(array('lon' => 37.62, 'lat' => 55.75)); $this->assertEquals($weather->city->country, 'RU'); }
/** * * @param Search $entity * @return unknown */ private function getWeather($city) { // Language of data (try your own language here!): $lang = 'en'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). $owm = new OpenWeatherMap(); try { $weather = $owm->getWeather($city, $units, $lang, 'a93a74642c5599fe5ec0f05adeb0374c'); } catch (OWMException $e) { //echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; //echo "<br />\n"; } catch (\Exception $e) { //echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'; //echo "<br />\n"; } return $weather; }
require_once 'plugins/Cmfcmf/vendor/autoload.php'; $lang = 'es'; $units = 'metric'; //$idplaya = -1; if (isset($_GET['id'])) { $idplaya = $_GET['id']; } //$json = ""; $json = '{ "beaches" : ['; $a = new stdClass(); $a->lat = '-6.8385667'; $a->lon = '-79.9376798'; $a->nombre = 'Pimentel'; $beaches = array(array('lat' => '-6.8385667', 'lng' => '-79.9376798', 'nombre' => 'Pimentel'), array('lat' => '-6.849837', 'lng' => '-79.937633', 'nombre' => 'Las Rocas'), array('lat' => '-6.878607', 'lng' => '-79.92499', 'nombre' => 'Santa Rosa'), array('lat' => '-6.933170', 'lng' => '-79.866226', 'nombre' => 'Puerto Eten'), array('lat' => '-6.768344', 'lng' => '-79.972566', 'nombre' => 'San José'), array('lat' => '-6.959094', 'lng' => '-79.851197', 'nombre' => 'Los Lobos'), array('lat' => '-6.669829', 'lng' => '-80.080443', 'nombre' => 'Naylamp'), array('lat' => '-6.886118', 'lng' => '-79.919041', 'nombre' => 'Hondo'), array('lat' => '-6.903357', 'lng' => '-79.89936', 'nombre' => 'Monsefú'), array('lat' => '-6.951439', 'lng' => '-79.861406', 'nombre' => 'Media Luna'), array('lat' => '-4.103835', 'lng' => '-81.054779', 'nombre' => 'Máncora'), array('lat' => '-4.132647', 'lng' => '-81.102343', 'nombre' => 'Vichayito'), array('lat' => '-4.176102', 'lng' => '-81.130362', 'nombre' => 'Órganos'), array('lat' => '-5.009676', 'lng' => '-81.066617', 'nombre' => 'Colán'), array('lat' => '-4.452993', 'lng' => '-81.286127', 'nombre' => 'Lobitos'), array('lat' => '-4.250222', 'lng' => '-81.230774', 'nombre' => 'Cabo Blanco'), array('lat' => '-5.130975', 'lng' => '-81.171469', 'nombre' => 'Yacila'), array('lat' => '-4.114521', 'lng' => '-81.079810', 'nombre' => 'Pocitas'), array('lat' => '-3.955955', 'lng' => '-80.955890', 'nombre' => 'Punta Sal'), array('lat' => '-3.678105', 'lng' => '-80.676531', 'nombre' => 'Zorritos'), array('lat' => '-8.104030', 'lng' => '-79.106577', 'nombre' => 'Huanchaco'), array('lat' => '-7.398315', 'lng' => '-79.571873', 'nombre' => 'Pacasmayo'), array('lat' => '-8.223446', 'lng' => '-78.980916', 'nombre' => 'Salaverry'), array('lat' => '-7.923984', 'lng' => '-79.307979', 'nombre' => 'El Brujo')); if (!isset($_GET['id'])) { foreach ($beaches as $id => $beach) { $owm = new OpenWeatherMap(); $beach = $beaches[$id]; $weather = $owm->getWeather(array('lat' => $beach['lat'], 'lon' => $beach['lng']), $units, $lang, '3353661db4e4a9b023409e84c4f7b467'); $json .= '{' . '"nombre" :"' . $beach['nombre'] . '", "lat" :' . $beach['lat'] . ', "lng":' . $beach['lng'] . ', "temp" :"' . $weather->temperature->now . '", "velviento" :"' . $weather->wind->speed . '" , "dirvie" :"' . $weather->wind->direction . '" ,"hum" :"' . $weather->humidity . '" , "pres" :"' . $weather->pressure . '"},'; } } else { $owm = new OpenWeatherMap(); $beach = $beaches[$idplaya]; $weather = $owm->getWeather(array('lat' => $beach['lat'], 'lon' => $beach['lng']), $units, $lang, '3353661db4e4a9b023409e84c4f7b467'); $json .= '{' . '"nombre" :"' . $beach['nombre'] . '", "lat" :' . $beach['lat'] . ', "lng":' . $beach['lng'] . ', "temp" :"' . $weather->temperature->now . '", "velviento" :"' . $weather->wind->speed . '" , "dirvie" :"' . $weather->wind->direction . '" ,"hum" :"' . $weather->humidity . '" , "pres" :"' . $weather->pressure . '"},'; } $json = trim($json, ','); $json .= ']}'; header('Content-Type: application/json'); print $json;
/** * Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast. * * @param array|int|string $query The place to get weather information for. For possible values see below. * @param \DateTime $start The \DateTime object of the date to get the first weather information from. * @param \DateTime|int $endOrCount Can be either a \DateTime object representing the end of the period to * receive weather history data for or an integer counting the number of * reports requested. * @param string $type The period of the weather history requested. Can be either be either "tick", * "hour" or "day". * @param string $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned. * @param string $lang The language to use for descriptions, default is 'en'. For possible values see below. * @return string */ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = null, $lang = null) { return $this->service->getRawWeatherHistory($query, $start, $endOrCount, $type, empty($units) ? $this->units : $units, empty($lang) ? $this->lang : $lang, $this->api_key); }
*/ use Cmfcmf\OpenWeatherMap; use Cmfcmf\OpenWeatherMap\Exception as OWMException; require_once __DIR__ . '/bootstrap.php'; $cli = false; $lf = '<br>'; if (php_sapi_name() === 'cli') { $lf = "\n"; $cli = true; } // Language of data (try your own language here!): $lang = 'de'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). $owm = new OpenWeatherMap(); $owm->setApiKey($myApiKey); // Example 1: Get current temperature in Berlin. $weather = $owm->getWeather('Berlin', $units, $lang); echo "EXAMPLE 1{$lf}"; // $weather contains all available weather information for Berlin. // Let's get the temperature: // Returns it as formatted string (using __toString()): echo $weather->temperature; echo $lf; // Returns it as formatted string (using a method): echo $weather->temperature->getFormatted(); echo $lf; // Returns the value only: echo $weather->temperature->getValue(); echo $lf;
<?php /** * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org . * * @license MIT * * Please see the LICENSE file distributed with this source code for further * information regarding copyright and licensing. * * Please visit the following links to read about the usage policies and the license of * OpenWeatherMap before using this class: * * @see http://www.OpenWeatherMap.org * @see http://www.OpenWeatherMap.org/terms * @see http://openweathermap.org/appid */ use Cmfcmf\OpenWeatherMap; require_once __DIR__ . '/bootstrap.php'; // Language of data (try your own language here!): $lang = 'en'; // Units (can be 'metric' or 'imperial' [default]): $units = 'metric'; // Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works). $owm = new OpenWeatherMap($myApiKey); // Example 1: Get hourly weather history between 2014-01-01 and today. $history = $owm->getWeatherHistory('Berlin', new \DateTime('2014-01-01'), new \DateTime('now'), 'hour', $units, $lang); foreach ($history as $weather) { echo 'Average temperature at ' . $weather->time->format('d.m.Y H:i') . ': ' . $weather->temperature . "\n\r<br />"; }
/** * Get station's datas. * * @return array OWM collected datas. * @since 2.0.0 */ public function get_datas() { $this->last_owm_warning = ''; $this->last_owm_error = ''; if (get_option('live_weather_station_owm_account')[1] == 1 || get_option('live_weather_station_owm_account')[0] == '') { $this->owm_datas = array(); return array(); } try { $this->synchronize_owm(); $this->owm_datas = array(); $stations = $this->get_located_stations_list(); $owm = new OpenWeatherMap(); foreach ($stations as $key => $station) { $values = $this->get_owm_datas_array($owm->getRawWeatherData(array('lat' => $station['loc_latitude'], 'lon' => $station['loc_longitude']), 'metric', 'en', get_option('live_weather_station_owm_account')[0], 'json'), $station, $key); if (isset($values) && is_array($values)) { $this->owm_datas[] = $values; } } $this->store_owm_datas(); } catch (Exception $ex) { if (strpos($ex->getMessage(), 'Invalid API key') > -1) { $this->last_owm_error = __('Wrong OpenWeatherMap API key.', 'live-weather-station'); } else { $this->last_owm_warning = __('Temporary unable to contact OpenWeatherMap servers. Retry will be done shortly.', 'live-weather-station'); } return array(); } return $this->owm_datas; }