#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,687
    Rep Power
    171

    Yahoo Weather XML


    Hi;
    The code beow creates this page.
    PHP Code:
    <?php
    $url 
    'http://weather.yahooapis.com/forecastrss?w=7226269';
    $c file_get_contents($url);
    $xml simplexml_load_string($c);
    print_r($c);
    ?>
    1 - How can I access condition and the temperture (in centigrade)?

    2 - I also like to know about this element yweather:forecast.What is it? Why is it two words? How I can access the attributes? For example Isolated Thunderstorms
    Code:
    <yweather:forecast day="Sun" date="31 Mar 2013" low="71" high="78" text="Isolated Thunderstorms" code="37" />
    Thanks
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    1.
    PHP Code:
    $units = (string)$xml->channel->children("yweather"true)->units["temperature"];
    $temp = (string)$xml->channel->item->children("yweather"true)->condition["temp"];
    if (
    $units == "F") {
        
    $temp = ($temp 32) * 9;

    2. That's namespaces. Haven't we talked about this before?
    That children above switches namespace: because normally you're in the empty namespace but the weather information is in the yweather (aka http://xml.weather.yahoo.com/ns/rss/1.0) namespace. And because accessing it like
    PHP Code:
    $xml->channel->{"yweather:units"
    but isn't supported (but would be a nice feature request).
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,687
    Rep Power
    171
    Hey thank you.

    1- It's not printing.

    2 - What I like to get someinfo about is "units". Can you please send me a link to info about that.
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,687
    Rep Power
    171
    Working
    PHP Code:
    <?php
    $url 
    'http://weather.yahooapis.com/forecastrss?w=7226269';
    $c file_get_contents($url);
    $elements = new SimpleXMLElement($c);
    $node $elements->channel->item;
    $children $node->children('http://xml.weather.yahoo.com/ns/rss/1.0');
    $condition $children->condition;
    $attributes $condition->attributes();
    echo 
    $attributes['date'] . " temperature " $attributes['temp'] ."F";
    Last edited by zxcvbnm; March 30th, 2013 at 05:58 AM.

IMN logo majestic logo threadwatch logo seochat tools logo