Web Hosting Talk







View Full Version : A Simple RSS Feed Phraser in PHP


adaml
07-17-2005, 07:32 AM
Heres a simple RSS Feed Phraser function. Its simple and effective function. All you need to do is add it to the page you wish to display the feed.

If you want to show / display the feed all you need to do is call up the function using:

showFeed($url,$count);

The $variables can be used to define a few options:

$url = the url of the rss feed (e.g. http://slashdot.org/slashdot.rdf)
$count = the number of titles and descriptions to display (e.g. 5)

Using this function you can display more than one rss feed in one page / file, without constantly rewriting out all the code to display the feed. The function:


function showFeed($url,$count)
{
//* get the xml file
$xml = simplexml_load_file($url);
//* phrase all the data from the xml so we can display it
for ($x = 0; $x < $count; $x++) {
//* this bits for if its an rss 1 feed
if (isset($xml->item)) {
$item = $xml->item[$x];
}
//* this bits for 0.91 rss feeds
elseif (isset($xml->channel->item)) {
$item = $xml->channel->item[$x];
}
//* show the headline and the description
echo "<a href=\"$item->link\">$item->title</a><br>$item->description<br><br>";
}
//* reset variables
unset($xml);
unset($item);
//* close the function loop
}



Enjoy!

Let me know if you need any help!

adaml
07-22-2005, 05:02 AM
Did anyone find this useful ?

anjanesh
08-28-2005, 01:20 AM
Works in PHP 5 only - for versions < 5, xml_parse (http://php.net/manual/en/function.xml-parse.php) function has to be used.

bigmac99
08-28-2005, 10:08 PM
is this true? it won't work with PHP4x? How about an example with PHP4x?

Thanks
Charles

adaml
08-28-2005, 10:22 PM
yes its true, $xml came proberly builtin in version 5. you can use fopen to read an xml feed though :)

but i still think php 5 has been through enough testing so its all stable :) and working.

bigmac99
08-28-2005, 10:27 PM
but my host only has php4 installed....no 5.

Charles

Purple Butterfly
09-02-2005, 02:10 AM
Thanks a lot.

anjanesh
09-02-2005, 02:15 AM
Originally posted by bigmac99
but my host only has php4 installed....no 5.

Charles
I've stopped asking hosts if they'll install PHP5. If a host still uses PHP4 now then its not worth hosting a new website there. The only reason hosting Cos still use PHP4 is because of compatibility for old PHP4 sites.

adaml
09-02-2005, 08:26 PM
PHP 5 has no problems handling PHP 4 Applications or sites. Well iv had no troubles yet with my hosting clients, and we upgraded to PHP 5 a while back now.

Just waiting for cPanel to fully support MySQL 5 :) As that can handle procedures!