Попытка получить элементы из Yahoo Weather XML - PullRequest
0 голосов
/ 13 декабря 2011

Попытка извлечь значение из элемента восхода из [yweather: astronomy] в Yahoo XML Doc.Опробовал различные комбинации в соответствии с:

echo $yweather->astronomy == 'sunrise';

Является ли получение значения из элемента восхода правильной терминологией?Изо всех сил пытается найти много способов помочь с использованием этой терминологии в Интернете.

Остальная часть кода работает так, как я хочу

Yahoo XML Doc - фрагмент

<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" 
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>Yahoo! Weather - New York, NY</title>
    <link>
      http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/*http://weather.yahoo.com/forecast/USNY0996_f.html
    </link>
    <description>Yahoo! Weather for New York, NY</description>
    <language>en-us</language>
    <lastBuildDate>Mon, 12 Dec 2011 1:50 pm EST</lastBuildDate>
    <ttl>60</ttl>
    <yweather:location city="New York" region="NY" country="US"/>
    <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
    <yweather:wind chill="40" direction="0" speed="5"/>
    <yweather:atmosphere humidity="37" visibility="10" pressure="30.54" rising="2"/>
    <yweather:astronomy sunrise="7:09 am" sunset="4:26 pm"/>

rssweather.php

<?php

// Get XML data from source
$feed = file_get_contents("http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f");

// Check to ensure the feed exists
if(!$feed){
die('Weather not found! Check feed URL');
}

$xml = new SimpleXmlElement($feed);

foreach ($xml->channel as $entry){
    echo "<strong>Description</strong> "; 
    echo $entry->description; 
    echo "<br /><strong>Collected on</strong> ";
    echo $entry->lastBuildDate;

    //Use that namespace
    $yweather = $entry->children("http://xml.weather.yahoo.com/ns/rss/1.0");
    echo "<br /><strong>Sunrise</strong> ";
    echo $yweather->astronomy == 'sunrise';

}
>?

1 Ответ

2 голосов
/ 05 февраля 2012

Мое окончательное решение

// Get and return sunrise time 
function get_sunrise_time(SimpleXMLElement $xml) {
    $weather['sunrise'] = $xml->channel->children('yweather', TRUE)->astronomy[1]->attributes()->sunrise;

    return $weather;
}
...