Skip to content

Commit

Permalink
Uploading first version
Browse files Browse the repository at this point in the history
  • Loading branch information
cmfcmf committed Jul 6, 2013
1 parent 3fa7339 commit 924d6e0
Show file tree
Hide file tree
Showing 5 changed files with 526 additions and 0 deletions.
194 changes: 194 additions & 0 deletions Examples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php
/**
* OpenWeatherMap-PHP-API — An 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/about
* @see http://www.OpenWeatherMap.org/copyright
*/

// Include api class
require_once('OpenWeatherMap.php');

// Language of data (try your own language here!):
$lang = 'de';

// Units (can be 'metric' or 'imperial' [default]):
$units = 'metric';

// Example 1: Get current temperature in Berlin.
$weather = OpenWeatherMap::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";

/**
* @notice In the example above we're using a "shortcut". OpenWeatherMap returns the minimum temperature of a day,
* the maximum temperature and the temperature right now. If you don't specify which temperature you want, it will default
* to the current temperature. See below how to access the other values. Notice that each of them has implemented the methods
* "getFormatted()", "getValue()", "getUnit()".
*/

// Returns the current temperature:
echo "Current: " . $weather->temperature->now;
echo "<br />\n";

// Returns the minimum temperature:
echo "Minimum: " . $weather->temperature->min;
echo "<br />\n";

// Returns the maximum temperature:
echo "Maximum: " . $weather->temperature->max;
echo "<br />\n";

/**
* @notice When speaking about "current" and "now", this means when the weather data was last updated. You can get this
* via a DateTime object:
*/
echo "Last update: " . $weather->lastUpdate->format('r');
echo "<br />\n";

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

/**
* @notice You can use the methods above to only get the value or the unit.
*/

echo "Pressure: " . $weather->pressure;
echo "<br />\n";
echo "Humidity: " . $weather->humidity;
echo "<br />\n";

// Example 3: Get today's sunrise and sunset times.
echo "<br /><br />\n\n\nEXAMPLE 3<hr />\n\n\n";

/**
* @notice These functions return a DateTime object.
*/

echo "Sunrise: " . $weather->sun->rise->format('r');
echo "<br />\n";
echo "Sunset: " . $weather->sun->set->format('r');
echo "<br />\n";

// Example 4: Get current temperature from coordinates (Greenland :-) ).
$weather = OpenWeatherMap::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);
echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n";

echo "City: " . $weather->city->name;
echo "<br />\n";

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

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

echo "Id: " . $weather->city->id;
echo "<br />\n";

echo "Name: " . $weather->city->name;
echo "<br />\n";

echo "Lon: " . $weather->city->lon;
echo "<br />\n";

echo "Lat: " . $weather->city->lat;
echo "<br />\n";

echo "Country: " . $weather->city->country;
echo "<br />\n";

// Example 7: Get wind information.
echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n";

echo "Speed: " . $weather->wind->speed;
echo "<br />\n";

echo "Direction: " . $weather->wind->direction;
echo "<br />\n";

/**
* @notice For speed and direction there is a description available, which isn't always translated.
*/

echo "Speed: " . $weather->wind->speed->getDescription();
echo "<br />\n";

echo "Direction: " . $weather->wind->direction->getDescription();
echo "<br />\n";

// Example 8: Get information about the clouds.
echo "<br /><br />\n\n\nEXAMPLE 8<hr />\n\n\n";

// The number in braces seems to be an indicator how cloudy the sky is.
echo "Clouds: " . $weather->clouds->getDescription() . " (" . $weather->clouds . ")";
echo "<br />\n";

// Example 9: Get information about precipitation.
echo "<br /><br />\n\n\nEXAMPLE 9<hr />\n\n\n";

echo "Precipation: " . $weather->precipitation->getDescription() . " (" . $weather->precipitation . ")";
echo "<br />\n";

// Example 10: Show copyright notice. WARNING: This is no offical text. This hint was made regarding to http://www.http://openweathermap.org/copyright .
echo "<br /><br />\n\n\nEXAMPLE 10<hr />\n\n\n";

echo $weather->copyright;
echo "<br />\n";

// 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 "<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 "<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 "<br />\n";

// Example 14: Using an api key:

# OpenWeatherMap::getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');
# OpenWeatherMap::getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json');
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2013 Christian Flach

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
52 changes: 52 additions & 0 deletions OpenWeatherMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* OpenWeatherMap-PHP-API — An 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/about
* @see http://www.OpenWeatherMap.org/copyright
*/

require_once('Util.php');

class OpenWeatherMap
{
const url = "http://api.openweathermap.org/data/2.5/weather?";

static public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
{
switch($query) {
case (is_array($query)):
if (!is_numeric($query['lat']) || !is_numeric($query['lon'])) {
return false;
}
$query = "lat={$query['lat']}&lon={$query['lon']}";
break;
case (is_numeric($query)):
$query = "id=$query";
break;
case (is_string($query)):
$query = "q=" . urlencode($query);
break;
default:
return false;
}
$url = self::url . "$query&units=$units&lang=$lang&mode=$mode";
if (!empty($appid)) {
$url .= "&APPID=$appid";
}

return file_get_contents($url);
}

static public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '')
{
return new Weather($query, $units, $lang, $appid);
}
}
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
OpenWeatherMap-PHP-Api
======================
An php api to parse weather data from [OpenWeatherMap.org](http://www.OpenWeatherMap.org). This api tries to normalise and abstract the data and remove inconsistencies.

If you are looking for a [Zikula](http://www.zikula.org) implementation, you may want to take a look at [cmfcmf/Weather](https://github.com/cmfcmf/Weather)

License
=======
MIT — Please see the [LICENSE file](https://github.com/cmfcmf/OpenWeatherMap-PHP-Api/blob/master/LICENSE) 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.**
- [OpenWeatherMap.org](http://www.OpenWeatherMap.org)
- [OpenWeatherMap.org/about](http://www.OpenWeatherMap.org/about)
- [OpenWeatherMap.org/copyright](http://www.OpenWeatherMap.org/copyright)

Roadmap
=======
- Add forecast functionality
- Tell the guys of [OpenWeatherMap.org](http://www.OpenWeatherMap.org) that you made such an api.
Loading

0 comments on commit 924d6e0

Please sign in to comment.