Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created April 1, 2012 04:54
Show Gist options
  • Save joshhartman/2271523 to your computer and use it in GitHub Desktop.
Save joshhartman/2271523 to your computer and use it in GitHub Desktop.

Revisions

  1. joshhartman created this gist Apr 1, 2012.
    61 changes: 61 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?php
    if(isset($_POST['zipcode']) && is_numeric($_POST['zipcode'])){
    $zipcode = $_POST['zipcode'];
    }else{
    $zipcode = '50644';
    }
    $result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=f');
    $xml = simplexml_load_string($result);

    //echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');

    $xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
    $location = $xml->channel->xpath('yweather:location');

    if(!empty($location)){
    foreach($xml->channel->item as $item){
    $current = $item->xpath('yweather:condition');
    $forecast = $item->xpath('yweather:forecast');
    $current = $current[0];
    $output = <<<END
    <h1 style="margin-bottom: 0">Weather for {$location[0]['city']}, {$location[0]['region']}</h1>
    <small>{$current['date']}</small>
    <h2>Current Conditions</h2>
    <p>
    <span style="font-size:72px; font-weight:bold;">{$current['temp']}&deg;F</span>
    <br/>
    <img src="http://l.yimg.com/a/i/us/we/52/{$current['code']}.gif" style="vertical-align: middle;"/>&nbsp;
    {$current['text']}
    </p>
    <h2>Forecast</h2>
    {$forecast[0]['day']} - {$forecast[0]['text']}. High: {$forecast[0]['high']} Low: {$forecast[0]['low']}
    <br/>
    {$forecast[1]['day']} - {$forecast[1]['text']}. High: {$forecast[1]['high']} Low: {$forecast[1]['low']}
    </p>
    END;
    }
    }else{
    $output = '<h1>No results found, please try a different zip code.</h1>';
    }
    ?>
    <html>
    <head>
    <title>Weather</title>
    <style>
    body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    }
    label {
    font-weight: bold;
    }
    </style>
    </head>
    <body>
    <form method="POST" action="">
    <label>Zip Code:</label> <input type="text" name="zipcode" size="8" value="" /><br /><input type="submit" name="submit" value="Lookup Weather" />
    </form>
    <hr />
    <?php echo $output; ?>
    </body>
    </html>