Skip to content

Commit 924d6e0

Browse files
committed
Uploading first version
1 parent 3fa7339 commit 924d6e0

File tree

5 files changed

+526
-0
lines changed

5 files changed

+526
-0
lines changed

Examples.php

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
*/
15+
16+
// Include api class
17+
require_once('OpenWeatherMap.php');
18+
19+
// Language of data (try your own language here!):
20+
$lang = 'de';
21+
22+
// Units (can be 'metric' or 'imperial' [default]):
23+
$units = 'metric';
24+
25+
// Example 1: Get current temperature in Berlin.
26+
$weather = OpenWeatherMap::getWeather('Berlin', $units, $lang);
27+
echo "EXAMPLE 1<hr />\n\n\n";
28+
29+
// $weather contains all available weather information for Berlin.
30+
// Let's get the temperature:
31+
32+
// Returns it as formatted string (using __toString()):
33+
echo $weather->temperature;
34+
echo "<br />\n";
35+
36+
// Returns it as formatted string (using a method):
37+
echo $weather->temperature->getFormatted();
38+
echo "<br />\n";
39+
40+
// Returns the value only:
41+
echo $weather->temperature->getValue();
42+
echo "<br />\n";
43+
44+
// Returns the unit only:
45+
echo $weather->temperature->getUnit();
46+
echo "<br />\n";
47+
48+
/**
49+
* @notice In the example above we're using a "shortcut". OpenWeatherMap returns the minimum temperature of a day,
50+
* the maximum temperature and the temperature right now. If you don't specify which temperature you want, it will default
51+
* to the current temperature. See below how to access the other values. Notice that each of them has implemented the methods
52+
* "getFormatted()", "getValue()", "getUnit()".
53+
*/
54+
55+
// Returns the current temperature:
56+
echo "Current: " . $weather->temperature->now;
57+
echo "<br />\n";
58+
59+
// Returns the minimum temperature:
60+
echo "Minimum: " . $weather->temperature->min;
61+
echo "<br />\n";
62+
63+
// Returns the maximum temperature:
64+
echo "Maximum: " . $weather->temperature->max;
65+
echo "<br />\n";
66+
67+
/**
68+
* @notice When speaking about "current" and "now", this means when the weather data was last updated. You can get this
69+
* via a DateTime object:
70+
*/
71+
echo "Last update: " . $weather->lastUpdate->format('r');
72+
echo "<br />\n";
73+
74+
// Example 2: Get current pressure and humidity in Hongkong.
75+
$weather = OpenWeatherMap::getWeather('Hongkong', $units, $lang);
76+
echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n";
77+
78+
/**
79+
* @notice You can use the methods above to only get the value or the unit.
80+
*/
81+
82+
echo "Pressure: " . $weather->pressure;
83+
echo "<br />\n";
84+
echo "Humidity: " . $weather->humidity;
85+
echo "<br />\n";
86+
87+
// Example 3: Get today's sunrise and sunset times.
88+
echo "<br /><br />\n\n\nEXAMPLE 3<hr />\n\n\n";
89+
90+
/**
91+
* @notice These functions return a DateTime object.
92+
*/
93+
94+
echo "Sunrise: " . $weather->sun->rise->format('r');
95+
echo "<br />\n";
96+
echo "Sunset: " . $weather->sun->set->format('r');
97+
echo "<br />\n";
98+
99+
// Example 4: Get current temperature from coordinates (Greenland :-) ).
100+
$weather = OpenWeatherMap::getWeather(array('lat' => 77.73038, 'lon' => 41.89604), $units, $lang);
101+
echo "<br /><br />\n\n\nEXAMPLE 4<hr />\n\n\n";
102+
103+
echo "Temperature: " . $weather->temperature;
104+
echo "<br />\n";
105+
106+
// Example 5: Get current temperature from city id. The city is an internal id used by OpenWeatherMap. See example 6 too.
107+
$weather = OpenWeatherMap::getWeather(2172797, $units, $lang);
108+
echo "<br /><br />\n\n\nEXAMPLE 5<hr />\n\n\n";
109+
110+
echo "City: " . $weather->city->name;
111+
echo "<br />\n";
112+
113+
echo "Temperature: " . $weather->temperature;
114+
echo "<br />\n";
115+
116+
// Example 6: Get information about a city.
117+
$weather = OpenWeatherMap::getWeather('Paris', $units, $lang);
118+
echo "<br /><br />\n\n\nEXAMPLE 6<hr />\n\n\n";
119+
120+
echo "Id: " . $weather->city->id;
121+
echo "<br />\n";
122+
123+
echo "Name: " . $weather->city->name;
124+
echo "<br />\n";
125+
126+
echo "Lon: " . $weather->city->lon;
127+
echo "<br />\n";
128+
129+
echo "Lat: " . $weather->city->lat;
130+
echo "<br />\n";
131+
132+
echo "Country: " . $weather->city->country;
133+
echo "<br />\n";
134+
135+
// Example 7: Get wind information.
136+
echo "<br /><br />\n\n\nEXAMPLE 7<hr />\n\n\n";
137+
138+
echo "Speed: " . $weather->wind->speed;
139+
echo "<br />\n";
140+
141+
echo "Direction: " . $weather->wind->direction;
142+
echo "<br />\n";
143+
144+
/**
145+
* @notice For speed and direction there is a description available, which isn't always translated.
146+
*/
147+
148+
echo "Speed: " . $weather->wind->speed->getDescription();
149+
echo "<br />\n";
150+
151+
echo "Direction: " . $weather->wind->direction->getDescription();
152+
echo "<br />\n";
153+
154+
// Example 8: Get information about the clouds.
155+
echo "<br /><br />\n\n\nEXAMPLE 8<hr />\n\n\n";
156+
157+
// The number in braces seems to be an indicator how cloudy the sky is.
158+
echo "Clouds: " . $weather->clouds->getDescription() . " (" . $weather->clouds . ")";
159+
echo "<br />\n";
160+
161+
// Example 9: Get information about precipitation.
162+
echo "<br /><br />\n\n\nEXAMPLE 9<hr />\n\n\n";
163+
164+
echo "Precipation: " . $weather->precipitation->getDescription() . " (" . $weather->precipitation . ")";
165+
echo "<br />\n";
166+
167+
// Example 10: Show copyright notice. WARNING: This is no offical text. This hint was made regarding to http://www.http://openweathermap.org/copyright .
168+
echo "<br /><br />\n\n\nEXAMPLE 10<hr />\n\n\n";
169+
170+
echo $weather->copyright;
171+
echo "<br />\n";
172+
173+
// Example 11: Get raw xml data.
174+
echo "<br /><br />\n\n\nEXAMPLE 11<hr />\n\n\n";
175+
176+
echo "<pre><code>" . htmlspecialchars(OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'xml')) . "</code></pre>";
177+
echo "<br />\n";
178+
179+
// Example 12: Get raw json data.
180+
echo "<br /><br />\n\n\nEXAMPLE 12<hr />\n\n\n";
181+
182+
echo "<code>" . htmlspecialchars(OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'json')) . "</code>";
183+
echo "<br />\n";
184+
185+
// Example 13: Get raw html data.
186+
echo "<br /><br />\n\n\nEXAMPLE 13<hr />\n\n\n";
187+
188+
echo OpenWeatherMap::getRawData('Berlin', $units, $lang, null, 'html');
189+
echo "<br />\n";
190+
191+
// Example 14: Using an api key:
192+
193+
# OpenWeatherMap::getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');
194+
# OpenWeatherMap::getRawData('Berlin', $units, $lang, 'Your-Api-Key-Here', 'json');

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2013 Christian Flach
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

OpenWeatherMap.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* OpenWeatherMap-PHP-API — An php api to parse weather data from http://www.OpenWeatherMap.org .
4+
*
5+
* @license MIT
6+
*
7+
* Please see the LICENSE file distributed with this source code for further
8+
* information regarding copyright and licensing.
9+
*
10+
* Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.
11+
* @see http://www.OpenWeatherMap.org
12+
* @see http://www.OpenWeatherMap.org/about
13+
* @see http://www.OpenWeatherMap.org/copyright
14+
*/
15+
16+
require_once('Util.php');
17+
18+
class OpenWeatherMap
19+
{
20+
const url = "http://api.openweathermap.org/data/2.5/weather?";
21+
22+
static public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
23+
{
24+
switch($query) {
25+
case (is_array($query)):
26+
if (!is_numeric($query['lat']) || !is_numeric($query['lon'])) {
27+
return false;
28+
}
29+
$query = "lat={$query['lat']}&lon={$query['lon']}";
30+
break;
31+
case (is_numeric($query)):
32+
$query = "id=$query";
33+
break;
34+
case (is_string($query)):
35+
$query = "q=" . urlencode($query);
36+
break;
37+
default:
38+
return false;
39+
}
40+
$url = self::url . "$query&units=$units&lang=$lang&mode=$mode";
41+
if (!empty($appid)) {
42+
$url .= "&APPID=$appid";
43+
}
44+
45+
return file_get_contents($url);
46+
}
47+
48+
static public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '')
49+
{
50+
return new Weather($query, $units, $lang, $appid);
51+
}
52+
}

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
OpenWeatherMap-PHP-Api
22
======================
3+
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.
4+
5+
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)
6+
7+
License
8+
=======
9+
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.
10+
11+
**Please visit the following links to read about the usage policies and the license of OpenWeatherMap before using this class.**
12+
- [OpenWeatherMap.org](http://www.OpenWeatherMap.org)
13+
- [OpenWeatherMap.org/about](http://www.OpenWeatherMap.org/about)
14+
- [OpenWeatherMap.org/copyright](http://www.OpenWeatherMap.org/copyright)
15+
16+
Roadmap
17+
=======
18+
- Add forecast functionality
19+
- Tell the guys of [OpenWeatherMap.org](http://www.OpenWeatherMap.org) that you made such an api.

0 commit comments

Comments
 (0)