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.

rss-to-html.png

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.

One usage example:

<?php
// output RSS feed to HTML
output_rss_feed('http://feeds.systutorials.com/ericfeed', 20, true, true, 200);
?>

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.

Eric Zhiqiang Ma

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:

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

    http://norsk.worldcityguides.net/

    Thanks a lot for the brilliant script!

    1. Hi Siggi,

      You will need some PHP debugging then.

      For the characters problem, you may take a look at this line which possible causes this:

      $title = str_replace(' & ', ' & ', $feed[$x]['title']);

      Or you may add additional character encoding/decoding function for the $title.

      For date format, you may change the format in this line:

      $date = date('l F d, Y', strtotime($feed[$x]['date']));
  2. Well, l’m not sure what I’m asking. It worked locally with wp feeds. But now in production is not. Is creating an empty rss2html- file with just one line on it.

    Thanks again.

  3. Seems to be an encoding error since the blog is in spanish. Does not happen with your blogs feed. Can you point me how to fix it?

    Thank you!

      1. Well, the feed validated with 2 recommendation warnings. Finally I created a new feed URL with Feedburner and now is working properly.

        Thank you!

  4. Worked fine. Straight-forward even though php is not my language (yet).

    I like the fact that you took the time to put in CSS class names. I went back to do it myself and constrain some image sizes on the feed I used and found the classes already there. Nice!

    Thanks,
    j

  5. Hi

    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 :-)

    Thanks

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

      1. I am not a coder as I mentioned earlier. How would I need to modify the code to pull the “featured” image instead of the image from the actual post content?

        Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *