• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

rss

SimplePie RSS Reader

February 27, 2014

If you’re thinking about adding syndication to your website, consider using SimplePie, a super fast and easy-to-use feed parser written in PHP. With SimplePie, adding a RSS feed to your site couldn’t be more easier. In this article, I will show you how to create a RSS page that will display a feed. It will display my blog’s RSS feed as default when nothing is clicked. I’ve included a couple of links that are clickable. When the links are clicked, the RSS feed for that link will be displayed.

First, download SimplePie.

Next, we need to create a file and copy the code below. The code is pretty much self-explanatory.

<?php 
// include SimplePie
require_once('php/autoloader.php');
 
// Create a new object called $feed
$feed = new SimplePie();
 
// Set the default to my blog's RSS at first run
if ($_GET[feed] == "") { $_GET[feed]="http://uly.me/feed/"; }
$feed->set_feed_url($_GET[feed]);
 
// Run SimplePie.
$feed->init();
 
// Send the result to show up on the browser
$feed->handle_content_type();
?>
 
<!-- Display the links -->
<a href="rssfeed.php?feed=http://news.yahoo.com/rss">Yahoo News</a> | 
<a href="rssfeed.php?feed=http://feeds.reuters.com/reuters/topNews">Reuter News</a>
 
<!-- Display the header -->
<div class="header">
  <h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1>
  <p><?php echo $feed->get_description(); ?></p>
</div>
 
<?php
// Display the feed inside a loop
foreach ($feed->get_items() as $item): ?>
<div class="item">
<li><a href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo $item->get_title(); ?></a></li>
  <p><?php echo $item->get_description(); ?></p>
  <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
</div>
<?php 
endforeach;
 
/* end of file */

<?php // include SimplePie require_once('php/autoloader.php'); // Create a new object called $feed $feed = new SimplePie(); // Set the default to my blog's RSS at first run if ($_GET[feed] == "") { $_GET[feed]="http://uly.me/feed/"; } $feed->set_feed_url($_GET[feed]); // Run SimplePie. $feed->init(); // Send the result to show up on the browser $feed->handle_content_type(); ?> <!-- Display the links --> <a href="rssfeed.php?feed=http://news.yahoo.com/rss">Yahoo News</a> | <a href="rssfeed.php?feed=http://feeds.reuters.com/reuters/topNews">Reuter News</a> <!-- Display the header --> <div class="header"> <h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1> <p><?php echo $feed->get_description(); ?></p> </div> <?php // Display the feed inside a loop foreach ($feed->get_items() as $item): ?> <div class="item"> <li><a href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo $item->get_title(); ?></a></li> <p><?php echo $item->get_description(); ?></p> <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p> </div> <?php endforeach; /* end of file */

See the demo. Just add your own CSS stylesheet and you’re ready to go.

Filed Under: PHP Tagged With: feed, rss, simplepie

Where Is Your RSS Feed?

November 1, 2013

RSS or Really Simple Syndication enable publishers to syndicate their data automatically. Readers can subscribe to the feed and content will be delivered automatically. Most websites, news feeds, blogs, forums, etc. nowadays run some kind of content management system or CMS. And most CMS, if not all, have built-in RSS feed generators.

But, why is it so hard to find the RSS feed for some websites? Why do readers have to go for a bunny hunt each time, just to find the RSS feed. If it’s not on the top of the page, it must be at the bottom. If it’s not on the front page, then clearly, it must be somewhere else on the site. Maybe, it’s searchable on the website. Well, try again. In some instances, there is no RSS at all. What a shame at this day and age.

If you do find it, will it work at all? You’ll be surprise, some RSS feeds don’t work at all. I’m not sure why publishers can’t adhere to the standard RSS format of either RSS 2.0 or Atom 1.0. Clearly, there must be a way to simplify the entire RSS hunt process. Maybe, modern browsers like Chrome, Firefox, Safari and IE can help out a bit in locating the RSS feeds of each website.

Browsers should be able to direct users to the RSS feed of each website, and make them accessible to minimize the hunt. There are several browser plugins or extensions that simplify the process. I use RSS Feed Reader for Chrome. Acquiring feeds for each website is not always automatic. I still have to type the domain, but it does simplify the process a bit.

Clearly, there has to be a better way. I hope Chrome, Firefox, or other browsers can help out.

Filed Under: HTML, PHP Tagged With: rss

  • Home
  • About
  • Search

Copyright © 2023