View on GitHub

randomhost/weather

Encapsulates functions for easy retrieval of weather data from the Yahoo Weather API.

Download this project as a .zip file Download this project as a tar.gz file

Build Status

Introduction

This package encapsulates functions for easy retrieval of weather data from the Yahoo Weather API. It was created as part of randomhost/webcamoverlay but is released as a separate component so it can be used in other packages.

Because it was created as a dependency of randomhost/webcamoverlay, it does only support a subset of the available weather data.

Usage

A basic approach at using this package could look like this:

<?php
namespace randomhost\Weather;

require_once '/path/to/vendor/autoload.php';

// get Yahoo Weather API Feed instance
$feed = new Yahoo\Feed('Cologne', Yahoo\Feed::UNITS_INTL);

echo sprintf(
    'Temperature: %s°%s, Humidity: %s%%',
    $feed->getCondition()->getTemperature(),
    $feed->getUnits()->getTemperature(),
    $feed->getAtmosphere()->getHumidity()
);

This will instantiate the class, fetch the weather data and display current temperature and humidity.

Assuming that you named this file weather.php, you should now be able to access the weather data at http://example.com/weather.php

A more detailed example can be found in src/www/weather.php.

The Feed object

The Feed object is the primary object you will interact with in your application. It provides all methods required for retrieving data from the Yahoo! Weather API.

Constructor

The constructor takes 3 parameters which are all optional:

If a $locationName is given, Feed::fetchData() will be called implicitly which will automatically retrieve weather data for the given location ID and populate the data access objects accordingly.
If you do not want this, you can omit $locationName or set it to '' (an empty string) and configure the location name yourself using Feed::setLocationName().

The $systemOfUnits parameter can be used to determine the system of units to be used by the weather feed. If not given or set to '' (an empty string), it defaults to using international units (Celsius, kilometers, millibars).

$feedUrl defines the feed URL for retrieving weather data from the Yahoo! Weather API. This parameter does not usually need to be changed unless Yahoo! changes their API.

Configuring the feed

The following public methods for configuring the weather feed are available:

Retrieving data from the feed

The following public methods for retrieving data from the feed are available:

The Data\Location object

The Data\Location object represents location information of the forecast.

The following public methods for retrieving data are available:

The Data\Units object

The Data\Units object represents units for various aspects of the forecast.

The following public methods for retrieving data are available:

The Data\Wind object

The Data\Wind object represents forecast information about wind.

The following public methods for retrieving data are available:

The Data\Atmosphere object

The Data\Atmosphere object represents forecast information about current atmospheric pressure, humidity, and visibility.

The following public methods for retrieving data are available:

The Data\Astronomy object

The Data\Astronomy object represents forecast information about current astronomical conditions.

The following public methods for retrieving data are available:

The Data\Condition object

The Data\Condition object represents the current weather conditions.

The following public methods for retrieving data are available:

The Data\Forecast object

The Data\Forecast object represents the weather forecast for a specific day.

The following public methods for retrieving data are available:

Attribution Guidelines

Please note that you are required to link back to Yahoo using the link URL provided by Feed::getLink() and one of the logos Yahoo provides. Please refer to Yahoo's Attribution Guidelines for Yahoo APIs for details.

License

See LICENSE.txt for full license details.