?
Solved

Display RSS / XML Data on a php page.

Posted on 2006-04-08
4
Medium Priority
?
202 Views
Last Modified: 2008-03-06
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... :)
0
Comment
Question by:FirstBorn
  • 2
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16410389
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-
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 16411365
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
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 16429719
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-
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 16433362
Hi Roonaan,

Wow!

These worked PERFECTLY!

Thank You VERY Much!

Respectfully,

FirstBorn
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question