added composer.json and needed libs

This commit is contained in:
Tobias Diekershoff 2015-07-11 20:21:09 +02:00
parent 4f1fb007c5
commit 1f74d409a2
42 changed files with 4413 additions and 0 deletions

View file

@ -0,0 +1,95 @@
<?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;
use Cmfcmf\OpenWeatherMap\AbstractCache;
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');
}
/**
* Example cache implementation.
*
* @ignore
*/
class ExampleCache extends AbstractCache
{
private function urlToPath($url)
{
$tmp = sys_get_temp_dir();
$dir = $tmp . DIRECTORY_SEPARATOR . "OpenWeatherMapPHPAPI";
if (!is_dir($dir)) {
mkdir($dir);
}
$path = $dir . DIRECTORY_SEPARATOR . md5($url);
return $path;
}
/**
* @inheritdoc
*/
public function isCached($url)
{
$path = $this->urlToPath($url);
if (!file_exists($path) || filectime($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;

View file

@ -0,0 +1,236 @@
<?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;
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:
echo $weather->temperature->getUnit();
echo "<br />\n";
/**
* 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";
/**
* 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 = $owm->getWeather('Hongkong', $units, $lang);
echo "<br /><br />\n\n\nEXAMPLE 2<hr />\n\n\n";
/**
* 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";
/**
* 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 = $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 = $owm->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 = $owm->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";
/**
* 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 created regarding to http://www.http://openweathermap.org/copyright .
echo "<br /><br />\n\n\nEXAMPLE 10<hr />\n\n\n";
echo $owm::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($owm->getRawWeatherData('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($owm->getRawWeatherData('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 $owm->getRawWeatherData('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 = $owm->getWeather("ThisCityNameIsNotValidAndDoesNotExist", $units, $lang);
} catch (OWMException $e) {
echo $e->getMessage() . ' (Code ' . $e->getCode() . ').';
echo "<br />\n";
}
// Try invalid $query.
try {
$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 = $owm->getWeather(-1, $units, $lang);
} 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";
}
// Example 15: Using an api key:
$owm->getWeather('Berlin', $units, $lang, 'Your-Api-Key-Here');

View file

@ -0,0 +1,69 @@
<?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;
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 "Sunrise : " . $forecast->sun->rise->format("H:i:s") . " Sunset : " . $forecast->sun->set->format("H:i:s");
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";
echo "---";
echo "<br />\n";
}
// Example 2: Get forecast for the next 3 days for Berlin.
$forecast = $owm->getWeatherForecast('Berlin', $units, $lang, '', 3);
echo "EXAMPLE 2<hr />\n\n\n";
foreach ($forecast as $weather) {
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') . "<br />";
echo $weather->temperature . "<br />\n";
echo "---<br />\n";
}

View file

@ -0,0 +1,43 @@
<?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;
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 = '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();
// 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 />";
}