PHP Classes

File: RSSfeeder.example.php

Recommend this page to a friend!
  Classes of Jon Gjengset   RSSfeeder   RSSfeeder.example.php   Download  
File: RSSfeeder.example.php
Role: Example script
Content type: text/plain
Description: Example file for the RSSfeeder class
Class: RSSfeeder
Parse and merge multiple RSS feeds
Author: By
Last change:
Date: 17 years ago
Size: 1,803 bytes
 

Contents

Class file image Download
<?php
//Include the feeder class
include('RSSfeeder.class.php');

//Get the URL to all feeds
$feedcont = array();
$feed[0] = $_GET['feed1'];
$feed[1] = $_GET['feed2'];
$feed[2] = $_GET['feed3'];
$feed[3] = $_GET['feed4'];

//Exit if no feeds are set
if (count($feed) == 0)
{
    die(
"<strong>No feeds given!</strong>");
}

//Make sure all feeds are valid
for ($i = 0; $i < count($feed); $i++)
{
    if (isset(
$feed[$i]) && (!$feedcont[$i] = file_get_contents($feed[$i])))
        die(
"<strong>Feed does not exist!</strong>");
   
    if (isset(
$feed[$i]) && (!strstr($feedcont[$i], '<rss version="2')))
        die(
"<strong>Only RSS v2 feeds currently supported!</strong>");
}

//Create the feed, and merge in all the other feeds given
//We could also have specified a second Sort-By parameter when constructing the RSSfeeder element
$rss = new RSSfeeder($feed[0]);
for (
$i = 1; $i < count($feed); $i++)
{
    if (isset(
$feed[$i]))
       
$rss->mergeWith($feed[$i]);
}

//Get an array with all the feeds (with sorting enabled)
$items = $rss->get(TRUE);

//die if no elements are returned
if (!is_array($items))
    die(
"<strong>Failed to retrieve feed!</strong>");


//And now we simply print the feeds
$ret = '<table cellspacing="0" cellpadding="0" border="0">'."\n";
$colors = array('#BBD9EE', '#C9E7FC');
$ccount = 0;
for (
$i = 0; $i < $maxelements; $i++)
{
   
$ret .= "\t".'<tr bgcolor="'.$colors[($ccount%2?0:1)].'">'."\n";
       
$ret .= "\t\t".'<td>'."\n";
           
$ret .= "\t\t\t".'<a href="'.$items[$i+$offset]['link'].'" alt="'.$items[$i+$offset]['title'].'">'.$items[$i+$offset]['title'].'</a><br />'."\n";
           
$ret .= "\t\t\t".$items[$i+$offset]['description']."\n";
       
$ret .= "\t\t".'</td>'."\n";
   
$ret .= "\t".'</tr>'."\n";
   
$ccount++;
}
$ret .= '</table>'."\n";
echo
$ret;
?>