简体   繁体   中英

yahoo weather Api

I would like to get the temperature value from Yahoo's weather API. I have found a tutorial but in the tutorial he is getting a different value. Could some one help me modify the tutorial that it could get the temp value from Yahoo's weather RSS feed?

<yweather:condition text="Partly Cloudy" code="30" temp="3"
                    date="Mon, 09 Apr 2012 3:48 pm EEST" />

RSS feed: http://weather.yahooapis.com/forecastrss?w=566473&u=c

The tutorial I followed: http://css-tricks.com/using-weather-data-to-change-your-websites-apperance-through-php-and-css/

If some one has a better solution for getting the value don't hesitate to say it. :)

This seems pretty straightforward. From the tutorial:

Since the only bit of information we care about is the yweather:condition element's text attribute, we're going to avoid creating an XML parsing object and use a short regular expression.

So, just look at the line with the regular expression:

 $weather_class = format_result( get_match( '/<yweather:condition text="(.*)"/isU', $data ) );

This is actually a bad regular expression because it assumes text will always be the first attribute (and that there'll always be that weird double-space. Here's a regular expression that will get the temp attribute regardless of where it falls:

/<yweather:condition\s+(?:.*\s)?temp="(.+)"/isU

Substitute that for the regular expression given to get_match() and you should be good to go.

Oh, and lest I be kicked off SO for not saying so: Attempting to parse arbitrary HTML XML with regular expressions is the path to madness .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM