Boulder Startup Week – PHP Demo

This code demonstrates:
- Using PHP to create a command-line interface utility.
- Passing in arguments from the command-line.
- Calling the Weather Underground REST API (returns XML).
- Extracting the weather text from the returned XML.

Thanks to Ryan Cook for organizing this Boulder Startup Week session.

#!/usr/local/php5/bin/php
<?php

/*****************************************************************************/
/* This demo was created by Neil Simon for Ryan Cooks PHP Hacking Session    */
/* Boulder Startup Week, 05/05/2010                                          */
/*****************************************************************************/
/* Module ........ get_forecast.php                                          */
/* Description ... (1) Uses PHP to create a command-line utility             */
/*                 (2) Passes arguments in from the command-line             */
/*                 (3) Calls the Weather Underground REST API (returns XML)  */
/*                 (4) Extracts the weather text from the returned XML       */
/* Usage ......... get_forecast.php {zip5}                                   */
/* Example ....... get_forecast.php 80304                                    */
/*****************************************************************************/

// First executable line, calls main()
main ($argc, $argv);

function main ($argc, $argv)
    {
    // Initialize to failure, set to 0 upon success
    $rc = 1;

    // Ex: $argv[0] get_forecast.php
    // Ex: $argv[1] 80304
    if ($argc != 2)
        {
        printf ("Usage ..... get_forecast.php {zip5}\n");
        printf ("Example ... get_forecast.php 80304\n");
        }
    else
        {
        // Extract option value
        $zip5 = $argv [1];

        // Get weather lines of text
        $weatherForecastLinesArray = array ();

        // Get lines of weather text
        if (getWeatherForecast ($zip5, $weatherForecastLinesArray) == 0)
            {
            // Display lines
            foreach ($weatherForecastLinesArray as $weatherForecastLine)
                {
                printf ("%s\n", $weatherForecastLine);
                }

            // Successful
            $rc = 0;
            }
        }

    // Returns status code to the operating system
    return ($rc);
    }

function getWeatherForecast ($zip5, &$weatherForecastLinesArray)
    {
    // Initialize to failure, set to 0 upon success
    $rc = 1;

    // Create a constant that points to the REST API
    define (WUNDERGROUND_API_URL,
    "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=$zip5");

    // Get the weather text -- returned as XML
    if (($wundergroundStr = file_get_contents (WUNDERGROUND_API_URL)) == FALSE)
        {
        printf ("ERROR: file_get_contents (WUNDERGROUND_API_URL) failed.\n");
        }
    else
        {
        // Create the XML array from the returned data
        $wundergroundXml = new SimpleXMLElement ($wundergroundStr);

        // Extract the text fields
        $forecastday = $wundergroundXml->txt_forecast->forecastday [0];
        $weatherForecastLinesArray [] = $forecastday->title;
        $weatherForecastLinesArray [] = $forecastday->fcttext . "\n";

        // Successful
        $rc = 0;
        }

    return ($rc);
    }

?>

Leave a Reply