Parsing delicious feed using SimplePie
First, I got help from some php ninjas (thank you a lot), SimplePie’s documentation and mailing list were useful too. Just that you know I know very little about php. Anyway I wanted to share this little thing with you.
I wanted add delicious feed in my links page. I was originally going to use Magpie RSS which I have used previously but ended up using SimplePie (I’m using version 1.3-dev). Download using the Download source link at github
End result will look like this:
This is nothing too exciting and I’d imagine pretty self-explanatory. I saved this as delicious.php and uploaded.
<?php
// All this requires is simplepie.class.php and here the path to it
require_once(’/path/to/simplepie.class.php’);
$feed = new SimplePie();
// Feed url
$feed->set_feed_url(‘http://feeds.delicious.com/username/…’);
// You need also “cache” folder
$feed->set_cache_location($_SERVER[‘DOCUMENT_ROOT’] . ‘/cache’);
$feed->handle_content_type();
$feed->set_cache_duration(3600);
$feed->init();
{
echo ‘<ul class=“del_links”>’;
foreach ($feed->get_items() as $item) {
echo ‘<li><a href=”’ .$item->get_link(). ‘”>’ .$item->get_title(). ‘</a><br />’;
// Tags (categories) are inside <span> and slightly smaller than link text
echo ‘<span class=“link_tags”>’;
foreach ($item->get_categories() as $category)
{
echo $category->get_label() . “ “;
}
echo ‘</span>’;
echo ‘</li>’; }
echo ‘</ul>’;
}
?>
Then I added this in my Habari theme, in page.single.php, so that if page title is Links it’ll shows my delicious links. If you add it for example your sidebar etc… basically you just need: <?php include('path/to/delicious.php'); ?>
<?php if ($post->title == “Links”) { ?>
<?php include(‘path/to/delicious.php’); ?>
<?php } ?>How I have styled the links (obviously you can do what you ever want, this is just an example):
.link_tags { font-size: 80%;}
.del_links {list-style-position: outside;}
.del_links li {padding-bottom: .5em;} I hope this makes some use for you. I might try later add also links in tags. :)
good stuff. i am trying to figure out how to change the date format in simple pie to just digits instead of words. i use it to pipe parse my identi.ca and lastfm feeds to my site.
@Billy: I'm using this as a base for parsing last.fm feed and what I simply did was to change
$item->get_date('j M, g:i a')to
$item->get_date('H:i - j/n/Y')and it's just numbers (00:58 - 5/5/2010). If that's what you mean.
this is exactly what i wanted. worked like a charm. thanks so much. you rock!