1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
3
4/**
5 * Basic example for the GlobalWeather-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: globalweather-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 or not being online
52$globalweather = &Services_Weather::service("GlobalWeather", array("debug" => 2));
53if (Services_Weather::isError($globalweather)) {
54    die("Error: ".$globalweather->getMessage()."\n");
55}
56
57/* Erase comments to enable caching
58$status = $globalweather->setCache("file", array("cache_dir" => "/tmp/cache/"));
59if (Services_Weather::isError($status)) {
60    echo "Error: ".$status->getMessage()."\n";
61}
62*/
63
64$globalweather->setUnitsFormat("metric");
65$globalweather->setDateTimeFormat("d.m.Y", "H:i");
66
67// First get code for location
68$search = $globalweather->searchLocation("Koeln / Bonn");
69if (Services_Weather::isError($search)) {
70    die("Error: ".$search->getMessage()."\n");
71}
72
73// Now iterate through available functions for retrieving data
74foreach (array("getLocation", "getWeather", "getForecast") as $function) {
75    $data = $globalweather->$function($search);
76    if (Services_Weather::isError($data)) {
77        echo "Error: ".$data->getMessage()."\n";
78        continue;
79    }
80
81    var_dump($data);
82}
83?>
84