Pull feeds into your website and customize them - gHacks Tech News

Pull feeds into your website and customize them

I'm building my personal page right now, and since I write 2 blogs of my own (with one more coming up) and contribute to 3 other blogs I thought it would be a nice feature to have a list of my most recent posts on my personal page. You could of course always update the list by hand, but how much easier it would be to get it done by a script. It turns out that this is harder to do than it sounds, but a tool I found, called Magpie RSS makes the job easy (or easier, rather).

If you want a simple job done quickly then download the tarball file (for Windows users, I know Tugzip will unpack it for you) and upload the four .inc files and the extlib directory to your server. You then just place some simple php code where you want the list and off you go.

There is one option that I found really useful, namely that it doesn't only understand a set number of tags. As long as they are in the following two forms Magpie will understand them. <field_name>value</field_name> <namespace:field_name>value</namespace:field_name> This is great if you need some customization, let me show you a quick example of what I needed and did.

Since on my upcoming home page I want to showcase my articles, I can't just pull feed items from gHacks for example, since this would include Martin's posts too. The fact that Magpie understands just about any tag came to my help though, along with a php if statement. The gHacks feed has a creator tag (<dc:creator></dc:creator>), which shows the author of the post. All I needed to do was to set Magpie to only show the posts where the creator is "Daniel". Here's the code.

$rss5 = fetch_rss( 'http://feeds.ghacks.net/ghacksnet' );
$items5 = array_slice($rss5->items, 0, $num_items);

foreach ( $rss5->items as $item ) {
$creator = ($item['dc']['creator']);
$href = $item['link'];
$title = $item['title'];
$created = $item['created'];
if ( $creator == 'Daniel' )
{echo "<li><a href=$href>$title</a> $created</li>\n";}
}

I'm very far from being a php expert, I just used what I found on forums and websites, so please forgive the lack of explanation, I'd rather not say anything than say something incorrect. My goal is to show you how simple Magpie is, using just a few lines of code, you can create aggregated RSS feeds for yourself without the hassle of third party widgets, which are mediocre at best. You can customize it any way you want, to show you only titles, the full posts, just the categories, etc. Take a look at the How-to section for some cool examples.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. Amr said on May 10, 2008 at 6:05 pm
      Reply

      I accomplished the same thing (well, maybe not exactly the same, but similar) using Google Reader. The way I did it was pretty simple, but I got exactly what I wanted. Here is what I did:

      1. The first step is to mark all the posts or articles that you want to include on your personal homepage as “shared”. Those items can be from different RSS feeds.

      2. Add a box to your homepage that displays your shared items. To do that, click on the “Shared items” link in the side menu, then click “Add a clip”. You’ll be able to set the number of items that appear, as well as the title of the box, and the colour scheme. It will generate a javascript snippet that you could just copy and paste in your homepage.

      3. Any other item that you mark as shared will now automatically appear on your homepage.

    2. Daniel said on May 10, 2008 at 9:15 pm
      Reply

      Hi Amr!

      Thanks for the tip, but I didn’t want to use any third party stuff. The reason is that this way, I can format the links any way I want to. I can include the tags that go with the post if I want to, or I can only show items with certain keywords.

      Also, I wanted to pull the items I wrote from gHacks, not all items.

      Google is a good choice though if you don’t mind the format imposed upon you by Google.

    3. Amr said on May 11, 2008 at 12:59 am
      Reply

      Sure Daniel, I totally understand what you mean about having complete control about the format, the tags, etc. I just thought of throwing this in here in case someone wants a quick and easy way to do this.

      Also, maybe I did not make this clear in my previous comment, but if you use the method I mentioned, you don’t have to pull whole feeds. You can choose specific items from each feed (by only marking those specific items as “shared”).

    4. Ralph du Plessis said on September 8, 2009 at 2:54 pm
      Reply

      Thanks Amr, That was nice and easy to follow and has formed the perfect template for me to give a client the step by step guide :-)

    Leave a Reply