...

View Full Version : File get contents from RSS feed help required!Please



ramki067
07-17-2008, 01:19 PM
Hi all,
I need to get the contents from yahoo webpage for Weather to be displayed in my webpage.My code is as below.


<?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.


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.

oesxyl
07-17-2008, 01:35 PM
try:


echo htmlentities($weather_feed);


everything is there, :)

regards

Iszak
07-17-2008, 01:42 PM
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

$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]);

?>

mlseim
07-17-2008, 01:46 PM
See this example .... weather underground instead of Yahoo:

http://www.catpin.com/weather

ramki067
07-17-2008, 01:52 PM
Thanks oesxyl.It worked.
But how do i extract the data as its like below, i need the forecast.


<?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 -->

ramki067
07-17-2008, 02:02 PM
Hey thanks Iszak.. it worked...

I got the below:


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


How do i extract only this:


Thu - Scattered Thunderstorms. High: 27 Low: 19

Thanks once again! Cheers.

Ramki.

oesxyl
07-17-2008, 02:05 PM
Thanks oesxyl.It worked.
But how do i extract the data as its like below, i need the forecast.


<?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
$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

oesxyl
07-17-2008, 02:08 PM
Hey thanks Iszak.. it worked...

I got the below:


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


How do i extract only this:


Thu - Scattered Thunderstorms. High: 27 Low: 19

Thanks once again! Cheers.

Ramki.

echo $matches[1][0];

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum