Returns the current weather at the place you specified.
public getWeather ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '' ) : |
||
$query | array | integer | string | The place to get weather information for. For possible values see below. |
$units | string | Can be either 'metric' or 'imperial' (default). This affects almost all units returned. |
$lang | string | The language to use for descriptions, default is 'en'. For possible values see http://openweathermap.org/current#multi. |
$appid | string | Your app id, default ''. See http://openweathermap.org/appid for more details. |
return | The weather object. There are three ways to specify the place to get weather information for: - Use the city name: $query must be a string containing the city name. - Use the city id: $query must be an integer containing the city id. - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. |
public function testUnauthorizedAccess() { try { $this->owm->getWeather('Paris'); } catch (OWMException $e) { $this->assertEquals(401, $e->getCode()); $this->assertRegExp('/^Invalid API key/', $e->getMessage()); } }
/** * 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); }
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; }
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; }
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) { $forecastAvg[] = [$f['start'] * 1000, $f['avg']]; $forecastLow[] = [$f['start'] * 1000, $f['low']];
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: echo $weather->temperature->getUnit(); echo "<br />\n";
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;
/** * Returns the current weather at the place you specified as an object. * * @param array|int|string $query The place to get weather information for. For possible values see below. * @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 OpenWeatherMap\CurrentWeather * @throws OpenWeatherMap\Exception */ public function getWeather($query, $units = null, $lang = null) { return $this->service->getWeather($query, empty($units) ? $this->units : $units, empty($lang) ? $this->lang : $lang, $this->api_key); }