Yahoo! Weather RSS Parsing


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. function retrieveYahooWeather($zipCode="84015") {
  3. $yahooUrl = "http://weather.yahooapis.com/forecastrss";
  4. $yahooZip = "?p=$zipCode";
  5. $yahooFullUrl = $yahooUrl . $yahooZip;
  6. $curlObject = curl_init();
  7. curl_setopt($curlObject,CURLOPT_URL,$yahooFullUrl);
  8. curl_setopt($curlObject,CURLOPT_HEADER,false);
  9. curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
  10. $returnYahooWeather = curl_exec($curlObject);
  11. curl_close($curlObject);
  12. return $returnYahooWeather;
  13. }
  14. $localZipCode = "84015"; // Clearfield, UT
  15. $weatherXmlString = retrieveYahooWeather($localZipCode);
  16. $weatherXmlObject = new SimpleXMLElement($weatherXmlString);
  17. $currentCondition = $weatherXmlObject->xpath("//yweather:condition");
  18. $currentTemperature = $currentCondition[0]["temp"];
  19. $currentDescription = $currentCondition[0]["text"];
  20. ?>

URL: weather, yahoo, rss, php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.