Display RSS / XML Data on a php page.

Hi,

I'm looking to extract data from an RSS and/or XML File (with our without an .xml or .rss extension) and have the Link Title displayed (as a Link, of course,) but one Title at a time at, for instance, 30 second increments.

Sort of like GMail has above the Inbox where it changes every so often.

Thank You VERY Much for Your Help.

Respectfully,

FirstBorn

PS.  I've attempted a few different scripts available online, but they don't do what I'm looking to have done listed above.
Thanks... :)
LVL 1
FirstBornAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Do you need the rss feed data to be refreshed automatically when you are on a certain page, or do you need just to cycle through items?

-r-
FirstBornAuthor Commented:
Hi Roonaan!

Great to See You!

Thanks for participating!

I would like for it to cycle through every 'xx' amount of seconds and refresh mayby every 'xx' amount of hours or whenever reloaded.
That would be Great!

One of the uses for this will be to display:
1.  XML Files on My Server that I write, and
2.  Ebay Auctions from specific users that I currently watch and write about.
3.  Any other rss/xml file that I may need in the future.
If You need a sample of one of the Ebay auctions that I keep an eye on and want displayed on My page,
here's one of them:
http://rss.api.ebay.com/ws/rssapi?FeedName=SearchResults&siteId=0&language=en-US&output=RSS20&fsop=1&fsoo=1&sass=gomarket1&frpp=25
(I have a few that I watch and want displayed, but this example usually has at least 5 to display in the reader,
and is the Best Example that I can come up with at this time.)

I would like the ability to just add any URL (for the rss/xml) to a file to use that with the reader on My page.
(Maybe in a separate config.inc.php page that sets the URLs of the rss/xml files and sets the refresh rate and recycle rate?)

Thank You VERY Much for Your Help, Again, Roonaan.
I'm Excited that You are Helping!

FirstBorn
RoonaanCommented:
FirstBorn,

The below code requires the xml library by Keith Devens: http://keithdevens.com/software/phpxml/xml.php, which is a handy tool to use when working with xml.

As I am not sure how you want your output I have created a function which returns the news items from different rss sources. What needs to happen next is up to you to tell.

<?php
   include 'xml.php';

  # The cache folder should be writable
  define('CACHE_DIR', dirname(__FILE__).'/cache/');
  # Cache expires after 60 minutes.
  define('CACHE_LIMIT', 60);
  # Script url
  define('SCRIPT_URL', $_SERVER['PHP_SELF']);
 
  # We have different feeds
  $feeds = array();
  $feeds[] = 'http://rss.api.ebay.com/ws/rssapi?FeedName=SearchResults&siteId=0&language=en-US&output=RSS20&fsop=1&fsoo=1&sass=gomarket1&frpp=25';
  //$feeds[] = 'http://othersite/feed.rss';
  //$feeds[] = 'http://othersite/feed.rss';
  //$feeds[] = 'http://othersite/feed.rss';
 
  # Retrieve the items
  $items = getFromFeeds($feeds, CACHE_LIMIT);
  echo '<marquee>';
  foreach($items as $i) {
     echo ' ['.htmlspecialchars($i['site'].' - <a href="'.$i['link'].'">'.$i['title'].'</a>] ';
  }
  echo '</marquee>';
   

  # Functions  
  function getFromFeeds($feeds, $cacheTimeOut=60) {
    $mainCache = CACHE_DIR.'/main.cache.php';
    # Chech the main cache file
    if(is_file($mainCache) && filemtime($mainCache) > strtotime('-'.$cacheTimeOut.' hours') && filemtime($mainCache) > filemtime(__FILE__)) {
      # Include the cache
      include $mainCache;
      # If items are found, return immediately
      if(is_array($items) && count($items) > 0) {
        return $items;
      }
    }
    $items = array();
    foreach($feeds as $feed) {
      $feed_items = getCachedFeedItems($feed, $cacheTimeOut);
      $items = array_merge($items, $feed_items);
    }
   
    # Writeback to Cache
    $f = fopen($mainCache,'w');
    if($f) {
      fwrite($f, '<'.'?php $items='.var_export($items,true).'; ?'.'>');
      fclose($f);
    }
   
    return $items;
  }
 
  function getCachedFeedItems($url, $cacheTimeOut=60) {
    $cacheFile = CACHE_DIR.'/'.preg_replace('/\W/', '', $url).'.txt';
    if(is_file($cacheFile) && filemtime($cacheFile) > strtotime('-'.$cacheTimeOut.' hours') && filemtime($cacheFile) > filemtime(__FILE__)) {
      include $cacheFile;
      if(is_array($items) && count($items) > 0) {
        return $items;
      }
    }
    $items = getRSSFeed($url);
   
    # Writeback to Cache
    $f = fopen($cacheFile,'w');
    if($f) {
      fwrite($f, '<'.'?php $items='.var_export($items,true).'; ?'.'>');
      fclose($f);
    }
   
    return $items;
  }
 
  function getRSSFeed($url) {
    $items = array();    
    $f = file_get_contents($url);
    $data = xml_unserialize($f);
    if(!isset($data['rss']['channel']['item'][0])) return array();
    foreach($data['rss']['channel']['item'] as $item) {
      $items[] = array('link'  => $item['link'],
                       'title' => $item['title'],
                       'date'  => isset($item['pubDate'])
                                  ? $item['pubDate']
                                  : '',
                       'site'  => $data['rss']['channel']['title'],
                       );
    }
    return $items;
  }
?>

I hope this helps, please post back when you have further questions.

Kind regards

-r-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FirstBornAuthor Commented:
Hi Roonaan,

Wow!

These worked PERFECTLY!

Thank You VERY Much!

Respectfully,

FirstBorn
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.