Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    File get contents from RSS feed help required!Please

    Hi all,
    I need to get the contents from yahoo webpage for Weather to be displayed in my webpage.My code is as below.

    Code:
    <?php
    error_reporting(E_ALL);
    $weather_feed = file_get_contents("http://xml.weather.yahoo.com/forecastrss?p=INXX0012&u=f");
    $weather = simplexml_load_string($weather_feed);
    echo "Weatherfeed is $weather_feed<br>";
    echo "Weather is $weather<br>";
    But i'm getting the below message only the weather string is displaying nothing! Not able to figure out why.

    Code:
    Weatherfeed is http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_f.html Yahoo! Weather for Bangalore, IN en-us Thu, 17 Jul 2008 7:00 am IST 60 142 18 http://weather.yahoo.com http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif 12.97 77.58 http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_f.html Thu, 17 Jul 2008 7:00 am IST INXX0012_2008_07_17_7_00_IST 
    Weather is
    Can anyone help please?

    Thanks,
    Ramki.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try:
    PHP Code:
    echo htmlentities($weather_feed); 
    everything is there,

    regards

  • #3
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    I'm guessing it's bad XML thus the parser doesn't work is why you're getting no result for the weather.. If you're only getting todays I rigged up a simple regex to get todays for you..
    PHP Code:
    <?php

    $feed 
    file_get_contents("http://xml.weather.yahoo.com/forecastrss?p=INXX0012&u=f");
    $feed strip_tags($feed);

    preg_match_all("#Forecast:\n(.*)\n#Uis"$feed$matches);
    print_r($matches[1]);

    ?>

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    See this example .... weather underground instead of Yahoo:

    http://www.catpin.com/weather

  • #5
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks oesxyl.It worked.
    But how do i extract the data as its like below, i need the forecast.

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <channel> <title>Yahoo! Weather - Bangalore, IN</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html</link> <description>Yahoo! Weather for Bangalore, IN</description> <language>en-us</language> <lastBuildDate>Thu, 17 Jul 2008 7:00 am IST</lastBuildDate> <ttl>60</ttl> <yweather:location city="Bangalore" region="" country="IN"/> <yweather:units temperature="C" distance="km" pressure="mb" speed="kph"/> <yweather:astronomy sunrise="6:02 am" sunset="6:50 pm"/> <image> <title>Yahoo! Weather</title> <width>142</width> <height>18</height> <link>http://weather.yahoo.com</link> <url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url> </image> <item> <title>Conditions for Bangalore, IN </title> <geo:lat>12.97</geo:lat> <geo:long>77.58</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html</link> <pubDate>Thu, 17 Jul 2008 7:00 am IST</pubDate> <description><![CDATA[<b>Forecast:</b><BR /> Thu - Scattered Thunderstorms. High: 27 Low: 19<br /> Fri - Scattered Thunderstorms. High: 27 Low: 19<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html">Full Forecast at Yahoo! Weather</a><BR/> (provided by The Weather Channel)<br/> ]]></description> <yweather:forecast day="Thu" date="17 Jul 2008" low="19" high="27" text="Scattered Thunderstorms" code="47" /> <yweather:forecast day="Fri" date="18 Jul 2008" low="19" high="27" text="Scattered Thunderstorms" code="38" /> <guid isPermaLink="false">INXX0012_2008_07_17_7_00_IST</guid> </item> </channel> </rss><!-- api1.weather.sp1.yahoo.com uncompressed Thu Jul 17 04:46:49 PDT 2008 -->

  • #6
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hey thanks Iszak.. it worked...

    I got the below:

    Code:
    Array ( [0] => Thu - Scattered Thunderstorms. High: 27 Low: 19 )

    How do i extract only this:

    Code:
    Thu - Scattered Thunderstorms. High: 27 Low: 19
    Thanks once again! Cheers.

    Ramki.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ramki067 View Post
    Thanks oesxyl.It worked.
    But how do i extract the data as its like below, i need the forecast.

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <channel>
    <title>Yahoo! Weather - Bangalore, IN</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html</link> <description>Yahoo! Weather for Bangalore, IN</description> <language>en-us</language> <lastBuildDate>Thu, 17 Jul 2008 7:00 am IST</lastBuildDate> <ttl>60</ttl> <yweather:location city="Bangalore" region="" country="IN"/> <yweather:units temperature="C" distance="km" pressure="mb" speed="kph"/> <yweather:astronomy sunrise="6:02 am" sunset="6:50 pm"/> <image> <title>Yahoo! Weather</title> <width>142</width> <height>18</height> <link>http://weather.yahoo.com</link> <url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url> </image> <item> <title>Conditions for Bangalore, IN </title> <geo:lat>12.97</geo:lat> <geo:long>77.58</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html</link> <pubDate>Thu, 17 Jul 2008 7:00 am IST</pubDate> <description><![CDATA[<b>Forecast:</b><BR /> Thu - Scattered Thunderstorms. High: 27 Low: 19<br /> Fri - Scattered Thunderstorms. High: 27 Low: 19<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html">Full Forecast at Yahoo! Weather</a><BR/> (provided by The Weather Channel)<br/> ]]></description> <yweather:forecast day="Thu" date="17 Jul 2008" low="19" high="27" text="Scattered Thunderstorms" code="47" /> <yweather:forecast day="Fri" date="18 Jul 2008" low="19" high="27" text="Scattered Thunderstorms" code="38" /> <guid isPermaLink="false">INXX0012_2008_07_17_7_00_IST</guid> </item> </channel> </rss><!-- api1.weather.sp1.yahoo.com uncompressed Thu Jul 17 04:46:49 PDT 2008 -->
    there are some solution:
    1. using xquery, build one or more xpath expression to extract only what you want
    2. after using
    PHP Code:
    $weather simplexml_load_string($weather_feed); 
    $whether is a object and can be manipulate very easy:

    http://www.php.net/manual/en/simplexml.examples.php

    regards

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ramki067 View Post
    Hey thanks Iszak.. it worked...

    I got the below:

    Code:
    Array ( [0] => Thu - Scattered Thunderstorms. High: 27 Low: 19 )

    How do i extract only this:

    Code:
    Thu - Scattered Thunderstorms. High: 27 Low: 19
    Thanks once again! Cheers.

    Ramki.
    PHP Code:
    echo $matches[1][0]; 
    regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •