PHP Class Cmfcmf\OpenWeatherMap

ファイルを表示 Open project: cmfcmf/openweathermap-php-api Class Usage Examples

Public Methods

Method Description
__construct ( string $apiKey = '', null | Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface $fetcher = null, boolean | string $cache = false, integer $seconds = 600 ) Constructs the OpenWeatherMap object.
getApiKey ( ) : string Returns the API Key.
getDailyWeatherForecast ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', integer $days = 1 ) : WeatherForecast Returns the DAILY forecast for the place you specified. DANGER: Might return fewer results than requested due to a bug in the OpenWeatherMap API!
getRawDailyForecastData ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', string $mode = 'xml', integer $cnt = 16 ) : string Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
getRawData ( $query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml' )
getRawHourlyForecastData ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', string $mode = 'xml' ) : string Directly returns the xml/json/html string returned by OpenWeatherMap for the hourly forecast.
getRawWeatherData ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', string $mode = 'xml' ) : string Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
getRawWeatherGroupData ( array $ids, string $units = 'imperial', string $lang = 'en', string $appid = '' ) : string Directly returns the JSON string returned by OpenWeatherMap for the group of current weather.
getRawWeatherHistory ( array | integer | string $query, DateTime $start, DateTime | integer $endOrCount = 1, string $type = 'hour', string $units = 'imperial', string $lang = 'en', string $appid = '' ) : string Directly returns the xml/json/html string returned by OpenWeatherMap for the weather history.
getWeather ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '' ) : CurrentWeather Returns the current weather at the place you specified.
getWeatherForecast ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', integer $days = 1 ) : WeatherForecast Returns the forecast for the place you specified. DANGER: Might return fewer results than requested due to a bug in the OpenWeatherMap API!
getWeatherGroup ( array $ids, string $units = 'imperial', string $lang = 'en', string $appid = '' ) : Array Returns the current weather for a group of city ids.
getWeatherHistory ( array | integer | string $query, DateTime $start, integer $endOrCount = 1, string $type = 'hour', string $units = 'imperial', string $lang = 'en', string $appid = '' ) : WeatherHistory Returns the weather history for the place you specified.
setApiKey ( string $apiKey ) Sets the API Key.
wasCached ( ) : boolean Returns whether or not the last result was fetched from the cache.

Private Methods

Method Description
buildQueryUrlParameter ( mixed $query ) : string Builds the query string for the url.
buildUrl ( $query, $units, $lang, $appid, $mode, string $url ) : boolean | string Build the url to fetch weather data from.
cacheOrFetchResult ( string $url ) : string Fetches the result or delivers a cached version of the result.
parseJson ( string $answer ) : stdClass
parseXML ( string $answer ) : SimpleXMLElement

Method Details

__construct() public method

Constructs the OpenWeatherMap object.
public __construct ( string $apiKey = '', null | Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface $fetcher = null, boolean | string $cache = false, integer $seconds = 600 )
$apiKey string The OpenWeatherMap API key. Required and only optional for BC.
$fetcher null | Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface The interface to fetch the data from OpenWeatherMap. Defaults to CurlFetcher() if cURL is available. Otherwise defaults to FileGetContentsFetcher() using 'file_get_contents()'.
$cache boolean | string If set to false, caching is disabled. Otherwise this must be a class extending AbstractCache. Defaults to false.
$seconds integer How long weather data shall be cached. Default 10 minutes.

getApiKey() public method

Returns the API Key.
public getApiKey ( ) : string
return string

getDailyWeatherForecast() public method

Returns the DAILY forecast for the place you specified. DANGER: Might return fewer results than requested due to a bug in the OpenWeatherMap API!
public getDailyWeatherForecast ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', integer $days = 1 ) : WeatherForecast
$query array | integer | string The place to get weather information for. For possible values see ::getWeather.
$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.
$days integer For how much days you want to get a forecast. Default 1, maximum: 16.
return Cmfcmf\OpenWeatherMap\WeatherForecast

getRawDailyForecastData() public method

Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
public getRawDailyForecastData ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', string $mode = 'xml', integer $cnt = 16 ) : string
$query array | integer | string The place to get weather information for. For possible values see ::getWeather.
$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.
$mode string The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default)
$cnt integer How many days of forecast shall be returned? Maximum (and default): 16
return string Returns false on failure and the fetched data in the format you specified on success. Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data.

getRawData() public method

Deprecation: Use {@link self::getRawWeatherData()} instead.
public getRawData ( $query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml' )

getRawHourlyForecastData() public method

Directly returns the xml/json/html string returned by OpenWeatherMap for the hourly forecast.
public getRawHourlyForecastData ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', string $mode = 'xml' ) : string
$query array | integer | string The place to get weather information for. For possible values see ::getWeather.
$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.
$mode string The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
return string Returns false on failure and the fetched data in the format you specified on success. Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data.

getRawWeatherData() public method

Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
public getRawWeatherData ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', string $mode = 'xml' ) : string
$query array | integer | string The place to get weather information for. For possible values see ::getWeather.
$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.
$mode string The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
return string Returns false on failure and the fetched data in the format you specified on success. Warning: If an error occurs, OpenWeatherMap ALWAYS returns json data.

getRawWeatherGroupData() public method

Only a JSON response format is supported for this webservice.
public getRawWeatherGroupData ( array $ids, string $units = 'imperial', string $lang = 'en', string $appid = '' ) : string
$ids array The city ids to get weather information for
$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 string Returns false on failure and the fetched data in the format you specified on success.

getRawWeatherHistory() public method

Directly returns the xml/json/html string returned by OpenWeatherMap for the weather history.
public getRawWeatherHistory ( array | integer | string $query, DateTime $start, DateTime | integer $endOrCount = 1, string $type = 'hour', string $units = 'imperial', string $lang = 'en', string $appid = '' ) : string
$query array | integer | string The place to get weather information for. For possible values see ::getWeather.
$start DateTime The \DateTime object of the date to get the first weather information from.
$endOrCount DateTime | integer Can be either a \DateTime object representing the end of the period to receive weather history data for or an integer counting the number of reports requested.
$type string The period of the weather history requested. Can be either be either "tick", "hour" or "day".
$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 string Returns false on failure and the fetched data in the format you specified on success. Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format.

getWeather() public method

Returns the current weather at the place you specified.
public getWeather ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '' ) : CurrentWeather
$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 Cmfcmf\OpenWeatherMap\CurrentWeather 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.

getWeatherForecast() public method

Returns the forecast for the place you specified. DANGER: Might return fewer results than requested due to a bug in the OpenWeatherMap API!
public getWeatherForecast ( array | integer | string $query, string $units = 'imperial', string $lang = 'en', string $appid = '', integer $days = 1 ) : WeatherForecast
$query array | integer | string The place to get weather information for. For possible values see ::getWeather.
$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.
$days integer For how much days you want to get a forecast. Default 1, maximum: 16.
return Cmfcmf\OpenWeatherMap\WeatherForecast

getWeatherGroup() public method

Returns the current weather for a group of city ids.
public getWeatherGroup ( array $ids, string $units = 'imperial', string $lang = 'en', string $appid = '' ) : Array
$ids array The city ids to get weather information for
$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 Array Array of CurrentWeather objects.

getWeatherHistory() public method

Returns the weather history for the place you specified.
public getWeatherHistory ( array | integer | string $query, DateTime $start, integer $endOrCount = 1, string $type = 'hour', string $units = 'imperial', string $lang = 'en', string $appid = '' ) : WeatherHistory
$query array | integer | string The place to get weather information for. For possible values see ::getWeather.
$start DateTime
$endOrCount integer
$type string Can either be 'tick', 'hour' or 'day'.
$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 Cmfcmf\OpenWeatherMap\WeatherHistory

setApiKey() public method

Sets the API Key.
public setApiKey ( string $apiKey )
$apiKey string API key for the OpenWeatherMap account.

wasCached() public method

Returns whether or not the last result was fetched from the cache.
public wasCached ( ) : boolean
return boolean true if last result was fetched from cache, false otherwise.