[PHP] RSS Feed Integration - Help!

ray_gillespie

OSNN Veteran Addict
Political Access
Joined
21 Mar 2002
Messages
1,693
What is the best way to integrate an RSS feed into a website? I'm using a website called RSSinfo at the moment to change the RSS feed into php which is then inserted into a page on my website, but I find this to be very slow a lot of the time. Is there anyway to set up some software on my server to change the RSS feed into php myself? I'm looking for a free solution if possible as the RSSinfo system I'm already using is free. Thanks in advance, reps to anyone that can help me.

PS This post was lost in the roll-back so apologies if anyone replied to the original post and I didn't see it in time.
 
a similar thread but about images, though some of the code I provided could be reasonably easily modified to do what you want.

Which version of PHP is on your server because the methods of doing this vary from version 4 to version 5

edit - the code above is for an RDF Feed, though a little bit of tweaking should make it able to do RSS2 or ATOM feeds
 
Hey thanks for that. I'm pretty new to RSS and PHP though, so this might take a long time for me to work all this out. I'll have a look and post back later. Thanks!
 
Last edited:
Been bored this morning, should really be revising for my Computer Languages exam, but to hell with it. Just worked with a computer language :D

here is a php4 class file which will parse an RSS2 file for the item title elements of the feed and return the titles in an array so long as it can open the RSS feed in the first place.

Currently it is required that a file in the same folder is writable by the apache/web user. It can be called anything, though in the examples its called 'temp.xml'. I am going to do a little bit of testing with another method of doing this to see if I can remove the need for the temp.xml file.

code is attached
 

Attachments

  • class.rss_parser.php
    6.3 KB · Views: 139
Last edited:
Why are you storing it into a variable by first downloading it, and then saving it, after saving it, you then stick it back into variable by freading it.

Check my work over please Geffy?
 

Attachments

  • class.rss_parser.php
    5.8 KB · Views: 108
Nevermind the above, it has an error, fixed one on the way.
 

Attachments

  • class.rss_parser.php
    5.8 KB · Views: 122
Latest version 1.2

doesnt require a storage file anymore. So no need for temp.xml


and to answer your question X (from the __store_xml function) we can destroy the instances of & because at present the parser only returns an array of the titles of the feeds, I could do the same thing to the & that I do to & now and change it to a XML safe place holder

Though, I have just noticed that I can very easily so the same thing to & as I do to & by just not changing & in the first place.

the __char_data function changes them back. Hopefully the placeholder is random enough not to get used in any normal RSS feed, though if you can think of a better one then let me know.
 

Attachments

  • class.rss_parser.php
    5.9 KB · Views: 109
harr, fixed mine as well Geffeh :p.

Hrm, XML parser should not choke on XML entities. So I am normally not happy with changing it to something which might be used by a valid XML feed.

Edit: Why are you using an array to store the XML file? Why not just store the entire thing in one string? That is what storing in a file and freading it would do. Fread would read the entire file into the string because of the fact that you give it the entire filesize to read.
 
First I would like to say that this is awesome, I doubt even MS get this level of tech support so quickly! Secondly, at the risk of sounding like a total n00b moron, I've managed to work out all I know about PHP and RSS by myself only from what I've needed to do so far, which hasn't been that much. However, this is getting quite complex and I'm stumbling at the starting block. I get to this code:

Code:
"class RSS_Parser {"

and every time I put the URL of the feed I get the following error:

Code:
"[B]Parse error[/B]: parse error, unexpected '=', expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in [B]/home/bgrovecs/public_html/testing.php[/B] on line [B]92"[/B]

I can't work out how to get http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/uk_politics/rss.xml into the php, it keeps asking for "}" all the time.

The code I've put in is as follows:
Code:
*/
class RSS_Parser { = new RSS_Parser('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/uk_politics/rss.xml', 6); $entry_titles = $rss_parser->fetchAndParseXML(); if (!$entry_titles) { die ("Unable to fetch and store RSS Feed data"); } else { print_r($entry_titles); }
    /**

Please forgive my ignorance, I appreciate that you guys obviously know a lot about this so this might be quite frustrating for you.
 
OK Version 1.3

Much quicker than 1.2, on par with 1.0

Most of the script run time is the downloading of the RSS feed

Current times for this version, in seconds (during DDoS attack)
Fetch Time: 1.05933809
Parse Time: 0.00824690
Total Time: 1.06852508

New times (no DDoS attack)
Fetch Time: 0.36030197
Parse Time: 0.00780511
Total Time: 0.36899495
 

Attachments

  • class.rss_parser.php
    5.7 KB · Views: 116
Last edited:
Firstly I would recommend getting the latest version which is attached to the post above.

OK what you need to do is this

PHP:
<?php
include "class.rss_parser.php";

$parser = new RSS_Parser('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/uk_politics/rss.xml', 6);
$array_of_items = $parser->fetchAndParseXML();

// you now have an array of news item titles. If you wanted a list
echo "<ul>\n";
foreach ($array_of_items as $title) {
    echo "<li>" . $title ."</li>\n";
}
echo "</ul>\n";
?>

The important thing to understand is that RSS_Parser is something called an Object. You treat it much like you would a string, integer or any other variable. I have hidden away the gritty inner workings of parsing RSS and exposed only three real functions for you to use and one constructor function.

The things you can do are
// create a new RSS_Parser object, so that you could parse a number of different
// feeds by creating different 'instances' or copies of the RSS_Parser
// This is the Constructor function, it must be called before any of the other functions
$parser = new RSS_Parser($url_of_feed, $number_of_entries);

// Tell the parser to download the feed
$parser->fetchXML();

// Tell the parser to parse the feed and return you an array of item titles
$array = $parser->parseXML();

// Tell the parser to download the feed and then parse it, returning you an array of item titles
$array = $parser->fetchAndParseXML();

pretty much anything beginning with a double underscore should be considered hidden and for use exclusively by the parser itself.

I hope that helps and doesn't sound too much like some ancient language :p
 
Last edited:
Ok, I think I'm getting slightly further :lick:
Now I get this:

Code:
[B]Warning[/B]: main(class.rss_parser.php): failed to open stream: No such file or directory in [B]/home/bgrovecs/public_html/testing.php[/B] on line [B]77[/B]

[B]Warning[/B]: main(class.rss_parser.php): failed to open stream: No such file or directory in [B]/home/bgrovecs/public_html/testing.php[/B] on line [B]77[/B]

[B]Warning[/B]: main(): Failed opening 'class.rss_parser.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in [B]/home/bgrovecs/public_html/testing.php[/B] on line [B]77[/B]

[B]Fatal error[/B]: Cannot instantiate non-existent class: rss_parser in [B]/home/bgrovecs/public_html/testing.php[/B] on line [B]79[/B]

What am I missing here?
 
what code are you using for testing.php

also have you downloaded the class.rss_parser.php file from above and placed that in the same directory as the testing.php file??
 
Yeah sorry I'd put it in the wrong directory, it works great! This is awesome!! My only question now is, how to I get the links in the feeds to work?
 
ah, yes that might be useful

urmm trying to add that to it now
 
And with that comes another release v1.4

Example
PHP:
<?php
include "class.rss_parser.php";

$parser = new RSS_Parser('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/uk_politics/rss.xml', 6);
$array_of_items = $parser->fetchAndParseXML();

// you now have an array of news item titles. If you wanted a list
echo "<ul>\n";
foreach ($array_of_items as $item) {
    echo "<li><a href=\"" . $item['link'] ."\">". $item['title'] ."</a></li>\n";
}
echo "</ul>\n";
?>

Sorry about all that. It should actually be reasonably usable now :p
 

Attachments

  • class.rss_parser.php
    6.9 KB · Views: 116
That is incredible, thanks so much for your help.

However (groan), I still have a couple of problems. If you don't have the time for this I completely understand, but incase you want to flex your php skills again, I need to do 3 things:

1) Have 2 RSS feeds on one page, each in a different cell of a table

2) Get the links to open in a new window

3) Show the sub-text for each link

The one I'm using at the moment allows me to do all of these, but is incredibly slow at times and often doesn't work at all, which is why I'm looking for a better solution that doesn't rely on a third-party website that may face heavy traffic etc. My existing page is here, which shows (if it loads) how I need it to look. The new page is here.
 
to have two feeds on one page do something like this

PHP:
<?php 
include "class.rss_parser.php"; 

$feed1_url = 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/uk_politics/rss.xml';
$feed2_url = 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/uk_technology/rss.xml';

$feed1_parser = new RSS_Parser($feed1_url, 6); 
$feed1_items = $feed1_parser->fetchAndParseXML(); 

// you now have an array of news item titles. If you wanted a list 
echo "<ul>\n"; 
foreach ($feed1_items as $item) { 
    echo "<li><a href=\"" . $item['link'] ."\">". $item['title'] ."</a></li>\n"; 
} 
echo "</ul>\n"; 

$feed2_parser = new RSS_Parser($feed2_url, 6); 
$feed2_items = $feed2_parser->fetchAndParseXML(); 

// you now have an array of news item titles. If you wanted a list 
echo "<ul>\n"; 
foreach ($feed2_items as $item) { 
    echo "<li><a href=\"" . $item['link'] ."\">". $item['title'] ."</a></li>\n"; 
} 
echo "</ul>\n"; 
?>

opening in a new window is your part, but something like this would do for the example above;
change
echo "<li><a href=\"" . $item['link'] ."\">". $item['title'] ."</a></li>
to
echo "<li><a href=\"" . $item['link'] ."\" target=\"_blank\">". $item['title'] ."</a></li>
think thats right anyway, not done "new window" links in ages

As for the sub-text that'll have to be another version which I can work on probably tomorrow as I have an exam today. Are there any other parts of the feed which you would like returned from the parser?
 
The only stuff I need from the feed is the main title, and then the following sentence, i.e.:

Cool response for EU budget plan
UK proposals to end the deadlock over the EU budget find little support among European leaders.

It doesn't matter too much about having them in different cells on the same page, I can just create 2 pages if it's easier.
 
Version 1.5
New Features
- Returns 'description' data
- Returns 'pubData' data

The above parts of the items are accessed by the above names just as the 'title' and 'link' parts are accessed.

Hope this version helps
 

Attachments

  • class.rss_parser.php
    7.6 KB · Views: 113

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back