homepage Welcome to WebmasterWorld Guest from 54.197.94.241
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / RSS, ATOM, and Related Technologies
Forum Library, Charter, Moderators: bill & werty

RSS, ATOM, and Related Technologies Forum

This 46 message thread spans 2 pages: < < 46 ( 1 [2]     
Short guide to include RSS on your website
using PHP to create html, readable by search engines
rincey




msg:1541994
 11:25 am on Jan 24, 2006 (gmt 0)

Including RSS feeds in to your own pages is pretty simple with PHP and some open source libraries. The follwing lines show how to create HTML from RSS feeds using the popular PHP tools magpierss (fetches and parses the RSS feed) and Smarty (template engine).

It may look like overkill to use such powerful tools for such a simple task. But once installed, both give a variety of functions and reduce the self-written code to a few lines.

Here we go:

A. Preparing the Webhost/Server

A current version of PHP is necessary. The following tutorial assumes that all files are placed in the folder /rssdemo on your web host.

1. Install magpierss RSS tool
I am not sure if I am allowed to add URLs here, so just google for "magpierss" and take the first result. Follow the DOWNLOAD link and download the recent version of magpierss (0.72 as I write this).

The file comes as .tar.gz format, so if you are on a Windows PC, you will need additional software to unzip this. I think WinZip does the job, you can also use the free tool "7-zip". The first "Extract" will make a .tar-file out of the .tar.gz, the second extraction should create a folder named magpierss-0.72.

2. Install Smarty template engine
Google for "smarty" and follow the link to php.net. Download the the recent version of Smarty (2.6.12 as I write this).

The file also comes as .tar.gz format, after extraction there should be a folder named Smarty-2.6.12.

3. Rename folders and move them to /rssdemo
Whereever you extracted both folders, now it is time to move them. First delete the version numbers out of the folder names. Change magpierss-0.72 to magpierss and Smarty-2.6.12 to Smarty. Move both folders into the /rssdemo folder on your local test server or upload them to your web server.

4. Smarty needs some special folders.
Please create the following folders in the /rssdemo folder:
- cache
- configs
- templates
- templates_c
The last one, templates_c, needs to have write permission for Smarty. If your webhost is on Unix, set the permissions as needed (777 in the worst case, depends on server config).

B. A few lines of PHP for parsing and displaying the feed.

Now, with all the installation work done, we only need a few more lines of code for our task. Place the follwing code in a file named feed.php in the /rssdemo folder.

------------------

<?
/* The folder where this file is located. Change to whatever you need */
$mydir = "/rssdemo";

/* Include magpierss and Smarty library */
require_once($_SERVER["DOCUMENT_ROOT"].$mydir."/Smarty/libs/Smarty.class.php");
require_once($_SERVER["DOCUMENT_ROOT"].$mydir."/magpierss/rss_fetch.inc");

/* Create a template object for further use */
$tpl = new Smarty();

/* Set folders for Smarty object. This folders have to exist on your web server (Check A.3) */
$tpl->template_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/templates/';
$tpl->compile_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/templates_c/';
$tpl->config_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/configs/';
$tpl->cache_dir = $_SERVER["DOCUMENT_ROOT"].$mydir.'/cache/';

/* The URL of the feed we want to include */
$url = "http://rss.news.yahoo.com/rss/world";

/* magpierss does all the work! */
$rss = fetch_rss($url);

/* Uncomment the following line to see the object and array data returned. Good to see which other information has been processed by magpierss */
// echo "<pre>"; print_r($rss); echo "</pre>";

/* If the RSS could be parsed, add it to the template */
if ($rss) {
/* Sends the feed title to the template engine */
$tpl->assign("feedtitle",$rss->channel["title"]);
/* Sends the RSS items as an array */
$tpl->assign("items",$rss->items);
}
else {
/* RSS problem? */
$tpl->assign("feedtitle","Problem with: $url");
}

/* Fill the template file itemlist.html with the information and return it */
$feedhtml = $tpl->fetch($_SERVER["DOCUMENT_ROOT"].$mydir."/templates/itemlist.html");
/* Do with $feedhtml whatever you want */
echo $feedhtml;
?>


------------------

C. We need a template!

Smarty is a very powerful template engine which makes creating tables and lists a snap. For this demo we need a simple template that prints the name of the feed in the title and lists the items.

Copy the following lines into a file named "itemlist.html" and save it in the /rssdemo/templates folder.

------------------

{* very simple template for a table with RSS items *}
<table>
{* Show the feed title as a header in the first row of the table *}
<tr><th style="font-size: 24px; border: 1px solid black;">{$feedtitle}</th></tr>
{* loop through all items and add date, link, title and description in the follwing section *}
{section name=x loop=$items}
<tr><td class="rssitem" style="border: 1px solid red;">
<strong>{$items[x].pubdate}: <a href="{$items[x].link}" target="_blank">{$items[x].title}</a></strong><br>
{$items[x].description}
</td></tr>
{/section}
</table>
{* That's it. Table can be formatted using some CSS classes *}


------------------

D. Putting it all together

Now browse to the URL /rssdemo/feed.php on your web server. The feed should be displayed with some red lines around each item.

E. Serious usage

Usually you want to include the feed output somewhere into your page. The simplest way is the name your pages .php instead of .html and add the follwing line where the feed html should be displayed.

<? include "rssdemo/feed.php"?>

e.g. like this

<table>
<tr>
<td width="80%">Left column with some information</td>
<td>Right column contains the feed<br /> <? include "rssdemo/feed.php"?></td>
</tr>
</table>

If you can't or don't want to rename your existing files, you could force your webserver to parse even .html-files for PHP code. If you use Apache, you could add the "AddType application/x-httpd-php .php .html" to your .htaccess. But this is another story.

I am sure I forgot some important things, so please feel free to ask. I installed both libraries and scripted and tested the PHP and template code as I wrote this on my local development server, so everything should work as expected.

 

Starbuck




msg:1542024
 4:51 pm on Apr 18, 2006 (gmt 0)

I changed the URL to this:

[rollingstone.com...]

and I got this error

Warning: MagpieRSS: Failed to fetch [rss.news.yahoo.com...] (HTTP Response: ) in D:\wwwRoot\*******.com\rss\magpierss\rss_fetch.inc on line 238

Since it was not letting me path deeper to the other location, I moved everything below the root. & tried the above.

trader




msg:1542025
 4:51 pm on Apr 18, 2006 (gmt 0)

The original post seems extremely complex, tedious and involved, and the later post #30 by Starbuck needing a rocket scientist to comprehend it all.

Quite surprised there is not an automated way to do this or a program you can buy which will do most of the work automatically. Is there such a thing?

Starbuck




msg:1542026
 10:09 am on Apr 19, 2006 (gmt 0)

anyone have any other comments?

Starbuck




msg:1542027
 5:18 pm on Apr 20, 2006 (gmt 0)

anyone know how to make this work?

Starbuck




msg:1542028
 11:07 pm on Apr 20, 2006 (gmt 0)

AQny feedback on what might be wrong?

trader




msg:1542029
 4:52 am on Apr 21, 2006 (gmt 0)

Looks like the answer is unfortuantely NO (to you and my post too).

Unfortunately, no response and unsettled issues often happen to many threads I have also participated in.

Starbuck




msg:1542030
 5:48 am on Apr 21, 2006 (gmt 0)

Well that is unfortunate. But I guess people are busy.

werty




msg:1542031
 10:05 am on Apr 21, 2006 (gmt 0)

You have to give people time to respond...

Based on the output that you recieved in post 30, it looks like magpie was able to parse the file, and it did properly output it. Did you properly configure smarty to format the output?

werty




msg:1542032
 10:12 am on Apr 21, 2006 (gmt 0)

Trader the answer to your post is that are numerous off the shelf packages that offer a solution to posting RSS feeds on your site. If you search any of the engines you will see paid solutions as well as get rich quick software that "will create 1000's of pages of content for you".

Starbuck




msg:1542033
 4:54 pm on Apr 21, 2006 (gmt 0)

Werty,

I understand that people are busy, I am grateful for any help.

I configured Smarty according to the instructions in this thread. that is all I did. Is there something else I should have done?

rincey




msg:1542034
 8:11 pm on Apr 23, 2006 (gmt 0)

Send me the template and PHP-file you created and I will look into this. I just stickied you my e-mail.

Starbuck




msg:1542035
 8:28 pm on Apr 24, 2006 (gmt 0)

which php file & which template file? I jsut followed the instructions on this thread. What file names exactly are you looking for?

aditogs




msg:1542036
 2:26 pm on Apr 26, 2006 (gmt 0)

Hi,

Trying to configure the output. At the moment my RSS feed is a google news one, and I can't seem to get it away from the default font size 12, which looks far toobig on my site.

I tried to edit the itemlist.htm, and it works for the date, but not for the rest. Do I need to reference the font-size elsewhere (where?)?

Please help as I've been stuck on this for days!

AT

my itemlist*

<table cellpadding="0" cellspacing="0">
{section name=x loop=$items}
<tr><td class="rssitem">

<font size="1" face="arial">
<strong>{$items[x].pubdate}: </strong>

<font size="1" face="arial"> {$items[x].description}

<hr size="1" width="500" align="center"> </td></tr>
{/section}
</table>

It works the first time but not the second. My css isn't upto scratch, so everything I've tried has no effect! Please help.

Starbuck




msg:1542037
 11:08 pm on May 5, 2006 (gmt 0)

Any further input on this? I still do not get anything usable.

Nytol




msg:1542038
 1:20 pm on May 19, 2006 (gmt 0)

Great tutorial.

I am trying to limit the number of headlines I fetch but I am having no luck whatsoever.

I am using the following code to try to limit the number of headlines but it doesn't seem to do anything:

$num_items = 3;
$rss = fetch_rss($url);

$items = array_slice($rss->items, 0, $num_items);

Starbuck




msg:1542039
 2:29 pm on May 19, 2006 (gmt 0)

Rincey,

Any luck on the review of the items I sent?

This 46 message thread spans 2 pages: < < 46 ( 1 [2]
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / RSS, ATOM, and Related Technologies
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved