Publishing RSS or XML feeds data with PHP is a great way of having fresh content on your website. They’re also a great way of updating your feeds across multiple website if you have a web service constantly generating fresh RSS feeds. You can also create your own RSS feeds reader on your website that can be accessed by you, by your team members and your friends. You can also gather news information by compiling XML feeds data from various sources.
PHP has a complete RSS processing routine that you can use to publish RSS feeds data, but there is a simple PHP class that helps you publish the feeds very easily on a web page, or even within a blog post (provided you can execute PHP code through blog posts). It is called SimplePie. Although there is a comprehensive guide available on the actual website, we’ll cut to the chase and simply learn here to publish a bulletted list of RSS feeds existing in an RSS link.
We assume you already have Apache and PHP running on your locahost. Download the simplepie.inc class and save it in the folder where you want to build the RSS feeds reader using PHP. Another thing is, in your root folder (something like htdocs or www) you need to have a folder named cache and change its permission so that it becomes server-writable. There are three types of values you can try: 755, 775 or 777. If you are trying this out on a server, you can use your FTP program to login, right-click on the cache folder, and change the permission values.
Then create a new file, and include this class, something like:
1 2 3 | <?php require_once "simplepie.inc"; ?> |
This makes simplepie.inc and its various functions available. For this example we’ll use the HowToPlaza RSS feeds link — you can use the link of your preference. Here’s the PHP code the publishes the RSS feeds using simplepie.inc class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $rssfeed = "http://feeds.feedburner.com/howtoplaza"; $feed = new SimplePie($rssfeed); $feed->init(); $feed->handle_content_type(); echo "<ul>"; foreach($feed->get_items() as $item) { echo "<li><a href='" . $item->get_permalink() . "'>" . $item->get_title() . "</a><br />" . $item->get_description() . "</li>"; } echo "</ul>"; $feed->__destruct(); unset($item); unset($feed); ?> |




