CakePHP plugin to get weather forecast information from yr.no
PHP
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Config/Schema
Console/Command
Lib
Model
Test/Case
.semver
README.md

README.md

CakePHP YrApi Plugin

Plugin that connects to the yr.no API for downloading forecast data.

Requirements

Installation

Download Plugin

Git Submodule

In your app directory type:

git submodule add git@github.com:nodesagency/YrApi.git app/Plugin/YrApi
git submodule init
git submodule update

Git Clone

In your plugin directory type:

git clone git://github.com/nodesagency/YrApi.git YrApi

Manual

Enable Plugin

Add the plugin to your app/Config/bootstrap.php file.

CakePlugin::load('YrApi');

Load database table

If you want to store the retrieved data from the API in your database, you can use the provided schema to build the database tables.

# Config/Schema/mysql.sql

Usage

YrApi

The Lib/YrApi class is a helper class for retrieving data from the API and returns data formatted to fit the models.

getForecast ( $postalCode )

Gets the forecasts for a specific postal code, returning an associative array with the data parsed from the API.

  • $postalCode (string) Norwegian postal code (4 digits) to retrieve the forecasts from

getPostalCodes ()

Gets the list of postal codes available in the yr.no API.

Examples

Get the forecast for the postal code 0445.

<?php
App::uses('YrApi', 'YrApi.Lib');
$yr = new YrApi();
$forecasts = $yr->getForecast('0445');
debug($forecasts);
Output
array(
	'Forecast' => array(
		(int) 0 => array(
			'from' => '2013-01-23T13:00:00',
			'to' => '2013-01-23T18:00:00',
			'period' => '2',
			'precipitation' => '0',
			'symbol_number' => '1',
			'symbol_name' => 'Klarvær',
			'symbol_var' => '01d',
			'wind_direction_deg' => '13.9',
			'wind_direction_code' => 'NNE',
			'wind_direction_name' => 'Nord-nordøst',
			'wind_speed_mps' => '1.4',
			'wind_speed_name' => 'Flau vind',
			'temperature_unit' => 'celsius',
			'temperature_value' => '-14',
			'pressure_unit' => 'hPa',
			'pressure_value' => '1016.5'
		),
		...
	)
)

Get a list of all the postal codes provided by the API.

<?php
App::uses('YrApi', 'YrApi.Lib');
$yr = new YrApi();
$codes = $yr->getPostalCodes();
debug($codes);
Output
array(
	'PostalCode' => array(
		(int) 0 => array(
			'id' => '0001',
			'city' => 'Oslo',
			'usage' => 'Postboksar',
			'municipality' => '301',
			'latitude' => '59.91160',
			'longitude' => '10.75450',
			'note' => 'Datakvalitet: 2. Koordinatar endra 28.09.2012. Oppdatert 04.12.2012',
			'nynorsk_url' => 'http://www.yr.no/sted/Norge/postnummer/0001/varsel.xml',
			'bokmaal_url' => 'http://www.yr.no/sted/Norge/postnummer/0001/varsel.xml',
			'english_url' => 'http://www.yr.no/sted/Norge/postnummer/0001/varsel.xml'
		),
		...
	)
)

Forecast Shell

The forecast shell provides shell access to the YrApi class and lets you store the results in the database (for running the shell as cronjobs).

Console/cake YrApi.Forecast codes [--store]

Gets the list of postal codes available in the yr.no API.

  • --store Store the results in the database instead of outputting the data
Console/cake YrApi.Forecast forecast [postalCode] [--delay] [--store]

Gets the forecast for a specific postal code or for all postal codes.

  • postalCode The postal code (4 digits) to retrieve the forecasts from
  • --delay Delay (for 2 seconds) between requests to avoid flooding the API
  • --store Store the results in the database instead of outputting the data