Exemple #1
0
/**
 * Test suitcase for the yr library
 * include it in the terminal
 *
 * $ php -a
 * php > include "test.php";
 */
// Include the classfiles once
include_once "Yr/Yr.php";
include_once "Yr/Forecast.php";
// This is not set by default in ini (OSX),
// so override to standard norwegian to avoid warnings
date_default_timezone_set("Europe/Oslo");
// Saving you
set_error_handler(function ($errno, $msg) {
    // Just print the error
    echo "PHP Error: " . $msg . "\n";
    // Do not go to internal php error handler
    return true;
});
// Possible to override default values
$location = !isset($location) ? "Norway/Vestfold/Sandefjord/Sandefjord" : $location;
$cache_path = !isset($cache_path) ? "/tmp" : $cache_path;
$cache_life = !isset($cache_life) ? 10 : $cache_life;
$language = !isset($language) ? "english" : $language;
// Initialize yr object
$yr = Yr\Yr::create($location, $cache_path, $cache_life, $language);
// Notice user if the yr object was created
if ($yr instanceof Yr\Yr) {
    print "An Yr object is now available on " . '$yr';
}
<?php

include __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "autoload.php";
$yr = Yr\Yr::create("Norway/Vestfold/Sandefjord/Sandefjord", "/tmp");
foreach ($yr->getHourlyForecasts(strtotime("now"), strtotime("tomorrow")) as $forecast) {
    echo sprintf("Time: %s, %s degrees\n", $forecast->getFrom()->format("H:i"), $forecast->getTemperature());
}
<?php

include __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "autoload.php";
$yr = Yr\Yr::create("Norway/Oslo/Oslo/Oslo", "/tmp", 10, null);
$weather_stations = $yr->getWeatherStations();
foreach ($weather_stations as $station) {
    print "Station: {$station->getName()}\n";
    print "Temperature: {$station->getForecast()->getTemperature()}\n";
    print "Wind Direction: {$station->getForecast()->getWindDirection()}\n";
    print "\n";
}
 public function setUp()
 {
     $this->yr = Yr\Yr::create("Norway/Oslo/Oslo/Oslo", "/tmp");
     $this->forecast = $this->yr->getCurrentForecast();
 }
 public function setUp()
 {
     $this->location = Yr\Yr::create("Norway/Oslo/Oslo/Oslo", "/tmp");
 }
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testCreateInvalidCachePath()
 {
     Yr\Yr::create("Norway/Oslo/Oslo/Oslo", "", 10, null);
 }
 public function setUp()
 {
     $this->yr = Yr\Yr::create("Norway/Oslo/Oslo/Oslo", "/tmp");
     $forecasts = $this->yr->getTextualForecasts();
     $this->forecast = reset($forecasts);
 }
<?php

include __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "autoload.php";
$yr = Yr\Yr::create("Norway/Oslo/Oslo/Oslo", "/tmp");
$forecast = $yr->getCurrentForecast();
printf("Time: %s to %s\n", $forecast->getFrom()->format("H:i"), $forecast->getTo()->format("H:i"));
printf("Temp: %s %s \n", $forecast->getTemperature(), $forecast->getTemperature('unit'));
printf("Wind: %smps %s\n", $forecast->getWindSpeed(), $forecast->getWindDirection('name'));
require_once './vendor/yr-php-library/autoload.php';
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
//echo '<pre>';
// buffer output so if anything fails, it wont display a partial calendar
$out = "BEGIN:VCALENDAR\r\n";
$out .= "PRODID:-//Permanent Solutions Ltd//Weather Calendar//EN\r\n";
$out .= "VERSION:5.1.4\r\n";
$out .= "CALSCALE:GREGORIAN\r\n";
$out .= "METHOD:PUBLISH\r\n";
$out .= "URL:https://github.com/allanlaal/weather-calendar-feed\r\n";
$out .= "X-WR-CALNAME:Weather\r\n";
$out .= "X-WR-CALDESC:Display yr.no weather forecasts in Google Calendar.\r\n";
$out .= "X-LOTUS-CHARSET:UTF-8\r\n";
// cache forecasts:
$yr = Yr\Yr::create($location, "./tmp");
$days = array();
foreach ($yr->getPeriodicForecasts(strtotime("now -3 days"), strtotime("now +100 days") - 1) as $forecast) {
    $days[$forecast->getFrom()->format('Y-m-d')][$forecast->getFrom()->format('H:i')] = $forecast;
}
foreach ($days as $date => $day) {
    $out .= "BEGIN:VEVENT\r\n";
    $out .= "DTSTART;VALUE=DATE:" . date('Ymd', strtotime($date)) . "\r\n";
    $out .= "DTEND;VALUE=DATE:" . date('Ymd', strtotime($date . ' +1 days')) . "\r\n";
    $out .= "DTSTAMP:" . date('Ymd\\THis\\Z') . "\r\n";
    $out .= "UID:Permanent-Weather-" . date('Ymd', strtotime($date)) . "-{$version}\r\n";
    $out .= "CLASS:PUBLIC\r\n";
    $out .= "CREATED:{$version}\r\n";
    $out .= "LOCATION:" . str_replace('/', ', ', $location) . "\r\n";
    //@https://www.ietf.org/rfc/rfc2445.txt
    $out .= "LAST-MODIFIED:{$version}\r\n";
 public function setUp()
 {
     $this->yr = Yr\Yr::create("Norway/Oslo/Oslo/Oslo", "/tmp");
     $stations = $this->yr->getWeatherStations();
     $this->station = reset($stations);
 }