1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */ 3 4/** 5 * Basic example for the METAR/TAF-service 6 * 7 * PHP versions 4 and 5 8 * 9 * <LICENSE> 10 * Copyright (c) 2005-2011, Alexander Wirtz 11 * All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * o Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * o Redistributions in binary form must reproduce the above copyright notice, 19 * this list of conditions and the following disclaimer in the documentation 20 * and/or other materials provided with the distribution. 21 * o Neither the name of the software nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 * </LICENSE> 37 * 38 * @category Web Services 39 * @package Services_Weather 40 * @author Alexander Wirtz <eru@php.net> 41 * @copyright 2005-2011 Alexander Wirtz 42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 43 * @version SVN: $Id: metar-basic.php 314111 2011-08-02 14:51:22Z eru $ 44 * @link http://pear.php.net/package/Services_Weather 45 * @filesource 46 */ 47 48require_once "Services/Weather.php"; 49 50// Object initialization - error checking is important, because of 51// handling exceptions such as missing PEAR modules 52$metar = &Services_Weather::service("METAR", array("debug" => 0)); 53if (Services_Weather::isError($metar)) { 54 die("Error: ".$metar->getMessage()."\n"); 55} 56 57// Set parameters for DB access, needed for location searches 58$metar->setMetarDB("sqlite://localhost//usr/local/lib/php/data/Services_Weather/servicesWeatherDB"); 59if (Services_Weather::isError($metar)) { 60 echo "Error: ".$metar->getMessage()."\n"; 61} 62 63/* Erase comments to enable caching 64$status = $metar->setCache("file", array("cache_dir" => "/tmp/cache/")); 65if (Services_Weather::isError($status)) { 66 echo "Error: ".$status->getMessage()."\n"; 67} 68*/ 69 70$metar->setUnitsFormat("custom", array( 71 "wind" => "kt", 72 "vis" => "km", 73 "height" => "ft", 74 "temp" => "c", 75 "pres" => "hpa", 76 "rain" => "in")); 77$metar->setDateTimeFormat("d.m.Y", "H:i"); 78 79// First get code for location 80$search = $metar->searchLocation("Bonn, Germany"); 81if (Services_Weather::isError($search)) { 82 die("Error: ".$search->getMessage()."\n"); 83} 84 85// Now iterate through available functions for retrieving data 86foreach (array("getLocation", "getWeather", "getForecast") as $function) { 87 $data = $metar->$function($search); 88 if (Services_Weather::isError($data)) { 89 echo "Error: ".$data->getMessage()."\n"; 90 continue; 91 } 92 93 var_dump($data); 94} 95?> 96