PHP RSS data extraction

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

PHP RSS data extraction

Post by mikeeeeeeey »

Hi guys,

I'm parsing an RSS feed from The Weather Network at the moment using SimpleXML (which is aweeesome!) in PHP.

Here's the code I have at the moment...

Code: Select all

$xml = simplexml_load_file('http://rss.theweathernetwork.com/weather/gbxx0005');
$dc = $xml->children();
echo $dc->channel->title;
echo "<br/><br/>";
echo $dc->channel->item->description;
...which outputs...

Code: Select all

The Weather Network - Sheffield, United Kingdom

Partly cloudy, 17 °C(62 °F) , Humidity 63% , Wind W 11km/h
This is fine and great and cool and allsorts, but all I really want is:
  • Region i.e. Sheffield, United Kingdom
  • Temperature i.e. 17 degrees
  • And the conditions so I can reference an image I've created OR a URL for an image created by the feed
I've tried exploding, seperating and outputting parts of the string but this seems like a very messy method.

Does anyone know if there's an easier way of extracting this data, or a Weather feed which is easier to chop up?

Thank youuuuuu.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I could be wrong but I think it's unlikely you'll find a better feed because the RSS standard doesn't define a way of marking up information about weather. I think you'll have to break up the parts with a regex. Splitting by comma is a good start.
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

Ahh fair enough. I think you're right ole, can't find anything useful online.
Thanks for the starting point too, I've managed to extract all the bits I need based on the commas.
Post Reply