Skip to content

Commit

Permalink
Made it PSR2 compatible, added composer, made it all object oriented,…
Browse files Browse the repository at this point in the history
… and added caching hooks. - BC-Breaks!
  • Loading branch information
cmfcmf committed Jul 21, 2013
1 parent 4b15295 commit 72ca319
Show file tree
Hide file tree
Showing 15 changed files with 624 additions and 301 deletions.
18 changes: 18 additions & 0 deletions Example_Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use cmfcmf\OpenWeatherMap;

require('cmfcmf/OpenWeatherMap.php');

// 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. See the example_cache file.
$owm = new OpenWeatherMap('examplecache', 100);

$weather = $owm->getWeather('Berlin', $units, $lang);
echo "EXAMPLE 1<hr />\n\n\n";
echo $weather->temperature;
43 changes: 24 additions & 19 deletions Examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
* @see http://openweathermap.org/appid
*/

// Include api class
require_once('OpenWeatherMap.php');
use cmfcmf\OpenWeatherMap;
use cmfcmf\OpenWeatherMap\Exception as OWMException;

require('cmfcmf/OpenWeatherMap.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 = OpenWeatherMap::getWeather('Berlin', $units, $lang);
$weather = $owm->getWeather('Berlin', $units, $lang);
echo "EXAMPLE 1<hr />\n\n\n";

// $weather contains all available weather information for Berlin.
Expand Down Expand Up @@ -73,7 +78,7 @@
echo "<br />\n";

// Example 2: Get current pressure and humidity in Hongkong.
$weather = OpenWeatherMap::getWeather('Hongkong', $units, $lang);
$weather = $owm->getWeather('Hongkong', $units, $lang);
echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n";

/**
Expand All @@ -98,14 +103,14 @@
echo "<br />\n";

// Example 4: Get current temperature from coordinates (Greenland :-) ).
$weather = OpenWeatherMap::getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang);
$weather = $owm->getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang);
echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n";

echo "Temperature: " . $weather->temperature;
echo "<br />\n";

// Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too.
$weather = OpenWeatherMap::getWeather(2172797, $units, $lang);
$weather = $owm->getWeather(2172797, $units, $lang);
echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n";

echo "City: " . $weather->city->name;
Expand All @@ -115,7 +120,7 @@
echo "<br />\n";

// Example 6: Get information about a city.
$weather = OpenWeatherMap::getWeather('Paris', $units, $lang);
$weather = $owm->getWeather('Paris', $units, $lang);
echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n";

echo "Id: " . $weather->city->id;
Expand Down Expand Up @@ -174,52 +179,52 @@
// Example 11: Get raw xml data.
echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n";

echo "<pre><code>" . htmlspecialchars(OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>";
echo "<pre><code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>";
echo "<br />\n";

// Example 12: Get raw json data.
echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n";

echo "<code>" . htmlspecialchars(OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'json')) . "</code>";
echo "<code>" . htmlspecialchars($owm->getRawData('Berlin', $units, $lang, null, 'json')) . "</code>";
echo "<br />\n";

// Example 13: Get raw html data.
echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n";

echo OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'html');
echo $owm->getRawData('Berlin', $units, $lang, null, 'html');
echo "<br />\n";

// Example 14: Error handling.
echo "<br /><br />\n\n\nEXAMPLE 14<hr />\n\n\n";

// Try wrong city name.
try {
$weather = OpenWeatherMap::getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
} catch(OpenWeatherMap_Exception $e) {
$weather = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
} catch(OWMException $e) {
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "<br />\n";
}

// Try invalid $query.
try {
$weather = OpenWeatherMap::getWeather(new DateTime('now'), $units, $lang);
} catch(Exception $e) {
$weather = $owm->getWeather(new \DateTime('now'), $units, $lang);
} catch(\Exception $e) {
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "<br />\n";
}

// Full error handling would look like this:
try {
$weather = OpenWeatherMap::getWeather(-1, $units, $lang);
} catch(OpenWeatherMap_Exception $e) {
$weather = $owm->getWeather(-1, $units, $lang);
} catch(OWMException $e) {
echo 'OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "<br />\n";
} catch(Exception $e) {
} catch(\Exception $e) {
echo 'General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "<br />\n";
}

// Example 15: Using an api key:

# OpenWeatherMap::getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');
# OpenWeatherMap::getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json');
# $owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');
# $owm->getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json');
Loading

0 comments on commit 72ca319

Please sign in to comment.