A PHP Function for Fetching RSS Feed and Outputing Feed Items as HTML

I aggregated all my new posts from several sites into a single RSS feed. Last time I updated my homepage, I came across an idea why not showing these feed items directly on the homepage as HTML content. After some study, I got one single PHP function in my hand that fetches an RSS feed and outputs HTML content in the page.

The PHP fuction and one example are as follows.

The single PHP function that gets RSS feed and converts it to HTML. Another helper function that outputs the HTML content.

The comments in the code should explain what it does well. You can call the output_rss_feed with the feed link and other options at the place where you would show the RSS feed.

This function also supports a simple caching mechanism so that not every call to the same RSS feed will invoke a network request. Note that there is no cache in this function and there is no planned future work to add the cache here. If you do meed the cache in a high volume site, consider adding a local cache system that caches the RSS feed and provide the function the new RSS address from your local cache system.

For the style of the items displayed, you may customize your site’s CSS file or add add a style tag to control the styles.

Please share if you like this post:

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

16 comments:

Hello there! I am using this on one site, but I have problems with the Norwegian characters in the titles of the blog posts, because the special norwegian letters æ, ø and å shows up in a not nice way. Do you know anything that can be done for this to work? And if I would want the dates of the publishing to show up following Norwegian standards, what can I do for this to happen?

I have used the code as it is (except to add the URL to my sites RSS feed). I get title, description, date and link no problem but no images. I have a jpg image in the post and I have another jpg image set as “featured”. Am I missing something here? Is there something I need to do in WP to make the image available on the RSS feed?

may be a simple answer but I am completely new to WP, RSS, PHP….everything really :-)

The image displayed is the one inside of the description part of the feed. If your feed has no image in the description, it is not displayed by this script. Please feel free to fork a copy and revise it according to your need.