Programmed Under The Influence
Parsing RSS feeds using SimpleXML

I’m was working with Last.fm’s API service to post user’s recent tracks on one of my sites. I wanted a lighter solution to using their REST Request method so I opted to use their RSS feeds instead. In the past I used either Magpie or SimplePie to parse the RSS for easy access, but I didn’t want the overhead of both these services so I did some research and found something of use courtesy of Stuart Herbert’s article, “Using SimpleXML To Parse RSS Feeds.”

I’ll give a brief tutorial on how I used Stuart’s tutorial to display my 10 most recent tracks from Last.fm:


// Get the RSS URL and plug in your ~username~
$lastfm_feed = "http://ws.audioscrobbler.com/1.0/user/~username~/recenttracks.rss";

// Turn the feed into a string and load it using PHP's simpleXML function
$raw_feed = file_get_contents($lastfm_feed);
$xml = simplexml_load_string($raw_feed);

// Next throw the results into a foreach loop to display the results
// I'll just display all the values you can print out
foreach ($xml->channel->item as $item) {
    echo $item->title; // Track title
    echo $item->link; // Link back to the track on Last.fm
    echo $item->pubDate; // Date the track was played
    echo $item->guid; // Unique identifier
    echo $item->description; // Links back to the artist's page
}