Posted By

tylerhall on 10/09/06


Tagged

xml yahoo weather scrape


Versions (?)

Who likes this?

15 people have marked this snippet as a favorite

simpeligent
m0rris
hudge
vali29
aaronmueller
silverbux
mmccrack
grn
sumandahal
irishsk
nb109
Hollow
neex123
tsturg15
tux-world


Get Yahoo! Weather Data


 / Published in: PHP
 

Grabs Yahoo!'s weather RSS feed for a given zipcode and extracts the current temperature and conditions, and the forecast for the next two days.

  1. $xml = geturl("http://xml.weather.yahoo.com/forecastrss?p=37205");
  2. preg_match_all('/<yweather:condition.*?text="(.*?)".*?code="(.*?)".*?temp="(.*?)".*?date="(.*?)"/mi', $xml, $current);
  3. preg_match_all('/<yweather:forecast.*?date="(.*?)".*?low="(.*?)".*?high="(.*?)".*?text="(.*?)".*?code="(.*?)"/mi', $xml, $forecast);
  4.  
  5. // To download every possible Yahoo! weather code (to find out the codes for rain), run...
  6. // curl http://us.i1.yimg.com/us.yimg.com/i/us/we/52/[0-50].gif -o weather_#1.gif
  7. $rain = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 35, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47);
  8.  
  9. $date = date("Y-m-d", strtotime($current[4][0]));
  10. $day0 = $current[3][0];
  11. $day0_text = $current[1][0];
  12. $day1 = $forecast[3][0];
  13. $day1_text = $forecast[4][0];
  14. $day2 = $forecast[3][1];
  15. $day2_text = $forecast[4][1];
  16. $rain = in_array($current[2][0], $rain) ? 1 : 0;

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: mahtab on May 8, 2007

T76YLKOI6

You need to login to post a comment.