|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Handy script for displaying RSS 1.0 headlines
There are a number of scripts and third party services available for displaying RSS data on a web page, but in my experience this is the easiest to use. I got it to work with no prior knowledge of PHP.
It's designed for RSS 1.0, and originally appeared on: www.sitepoint.com/article/php-xml-parsing-rss-1-0 I've mentioned this in an posting elsewhere before, but I tought it would be a good idea to make it available here in the new RSS/Tags/Blogs forum. The good thing about this script is that, although its written in php, it can be inserted into pages with other extensions (e.g. .html) with just a little work on the .htacccess file (or on the control panel for IIS users). More about this towards the end of the post. Firstly, the script itself. The following version of the script displays headlines only: <?php $insideitem = false; $tag = ""; $title = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title))); printf("<dd>%s</dd>",htmlspecialchars(trim($description))); $title = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $link; if ($insideitem) { switch ($tag) { case "TITLE": $title .= $data; break; case "LINK": $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://ENTER YOUR RSS FILE URL HERE","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?> And this version displays headlines and descriptions: <?php $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title))); printf("<dd>%s</dd>",htmlspecialchars(trim($description))); $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { switch ($tag) { case "TITLE": $title .= $data; break; case "DESCRIPTION": $description .= $data; break; case "LINK": $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen("http://ENTER YOUR RSS FILE URL HERE","r") or die("Error reading RSS data."); while ($data = fread($fp, 4096)) xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); fclose($fp); xml_parser_free($xml_parser); ?> In both cases, just replace the text 'ENTER YOUR RSS FILE URL HERE' with the location of the RSS file you wish to use. To get this to work on a page with a .htm/.html extension, make the following adjustments: Add the following lines of code to a .htaccess file (or create one if it isn't there already): Removehandler .html .htm AddType application/x-httpd-php .php .htm .html If your site is hosted on IIS rather than Apache, .htaccess files will not work. Instead, there should be a facility to add additional file extensions to the php interpreter. Add .htm and/ or .html to the list as required. And that's it. Hope this might be of some use to someone. |
|
#2
|
|||
|
|||
|
Hi
My server is Apache, and I cannot find a file like that (htaccess) on my server. How do I create the file and are you sure this wont mess with my site at all? Thanks for your help!! |
|
#3
|
|||
|
|||
|
The .htaccess file is literally a file written in something like wordpad or notepad, and named .htaccess . There isn't generally one on the server already, you have to create it and put it there. Place it in your site's root folder.
One important point is that your server must support PHP for this to work. It would be an exception nowadays for any hosting server not to support PHP. It shouldn't affect your site otherwise in any way. What this does is send your .html pages through the PHP interpreter on the server, so that the php code will work within the .html pages. Any probs, PM me and I will try to help. |
|
#4
|
||||
|
||||
|
RSS test page: http://www.vegasonesource.com/rss2.htm
After talking with Globat tech support I was advised the following commands are not allow in their .htaccess files. Any advice? Ken: This are the list of directives that are not supported Ken: Action Ken: AddCharSet Ken: AddEncoding Ken: AddLanguage Ken: AddType Ken: BrowserMatch Ken: BrowserMatchNoCase Ken: CookieTracking Ken: DefaultLanguage Ken: DefaultType Ken: ErrorDocument Ken: ErrorHeader Ken: Header Ken: LanguagePriority Ken: Options Ken: PassEnv Ken: Redirect Ken: RedirectMatch Ken: RedirectPermanent Ken: RedirectTemporary Ken: SetEnv Ken: SetEnvIf Ken: SetEnvIfNoCase Thanks! Mike
__________________
See that search box? It's there for a reason. Maybe, just maybe, the solution to your problem FASTER. Something to consider.... Gay Las Vegas Discount Las Vegas show ticket Gay hotel Las Vegas Gay Lesbian Wedding Las Vegas Last edited by mhnlv : August 25th, 2005 at 10:46 PM. Reason: Updated |
|
#5
|
|||
|
|||
|
Quote:
If they don't allow the commands to be added to the .htaccess, there isn't much that can be done. It's not something that can be worked around to the best of my knowledge. You would probably be better to use one of the third party scipts like rss2html. ( http://www.feedforall.com/free-php-script.htm ). If you really wanted to use the script above, and you don't want to move hosting provider, you could in open a separate account elsewhere with just the rss file and a page with your layout template and the script. You could then link to it from your other pages saying something like "See latest news here". However, you still would not be able to include the script on any of your existing pages. |
|
#6
|
|||
|
|||
|
Just leave as a PHP file?
Why not just leave it as a .php file rather than a .htm/l? Then you would not need the command you're not allowed to use in the .htaccess file.
Alternatively, the script could be re-written to save the output in to a .html file, and run via a cronjob or by polling a .php file yourself by some other automated means. Admittedly, that's not exactly the drop-in solution first offered.
__________________
Work: Web Positioning Centre ---- Spiderability test & keyword report: Spider Test |
|
#7
|
||||
|
||||
|
Thanks for the input! But you guys are talking way over my head.... I'm a FrontPage cut and paste kinda webmaster LOL. Anyhow, I did find a shortterm solution from http://www.rss-info.com/en_rssinclude-simple.html
So I just created a couple of different RSS pages and use the buttons to guide the uses to them. Clunky, but it works for now. Mike |
|
#8
|
|||
|
|||
|
A meta tag you can also use
To help some software find the feed, you can put this in the <head> area of the page:
<link rel="alternate" type="application/rss+xml" title="RSS 0.91" href="http://www.example.com/feed.rss" /> Change 'RSS 0.91' to whatever your type of RSS is, and the href to whatever the URL of your feed is. This makes the 'live bookmarks' symbol come up in Firefox, and helps some auto-find software find your feed. I'm sure Frontpage will let you put it in as a custom meta tag or something, or you might have to use whatever it has as a code view to put it in there. |
|
#9
|
|||
|
|||
|
PaulS,
Thanks for the ideas. The idea of keeping the file as a .html was related to situations where an existing page is having a feed parser added to it. Sometimes backlinks will point specifically at www.sitename.com/index.html, rather than just www.sitename.com. Changing to a .php file might impact SERP's in this type of situation. Obviously, if it is a new website under development, or the feed is being added to a page of its own, creating the page as .php makes perfect sense. |
|
#10
|
|||
|
|||
|
Quote:
Ah, of course, I should have guessed that. In that case using PHP to create/save a new version of the .html file at regular intervals would be an easy way to get the same effect, without having the problem with the host not allowing the .htaccess change. Although to do this efficiently it would be best to run a cronjob to run the PHP file, and they might not allow you to set up one of those either. |
|
#11
|
|||
|
|||
|
Quote:
What would I need to add to the code to get multiple feed sources onto one feed? For example: yahoo search feed on several keywords? |
|
#12
|
|||
|
|||
|
andrenym00,
I'm sorry but I don't know how you might incorporate multiple feeds into the script so that they appear as they are published. The only thing I might suggest is that you could place the script on your page more than once, and write in the feed that you want each time. That way, all the items from each feed would appear together, e.g. Feed 1 story 1 Feed 1 story 2 Feed 2 story 1 Feed 2 story 2 Feed 2 story 3 Feed 3 story 1 etc... However, I don't think that's what you want. Maybe if you read some of the other threads in this forum you might get a feel for some of the other scripts that are around. One of them might fit your requirements better.
__________________
That's all very well in practice, but will it work in theory? Visit MadeForOne.com, which markets personalized gifts in many different categories. New to SEOChat? Look at the FAQ's for answers to many of the most common questions. |
|
#13
|
|||
|
|||
|
Dragging up a very old thread here, but how could this script be modified to only display the first n entries from an RSS feed, rather than every entry?
|
![]() |
| Viewing: SEO Chat Forums > Search Engine Strategies > Blogs, Tagging, RSS Feeds > Handy script for displaying RSS 1.0 headlines |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |