Search code examples
phpmysqlsimplepie

Simplepie RSS feeds to MySQL database server


I am currently using Simplepie to pull in my RSS feeds as shown in the configuration below. I want to move the $url to my database because my site loads way too slow. I have each url as a key value pair to the site's name. I want to keep this association because I use for instance "abc" to pull the image out of my directory which I use to style it for each feed, as you can see in the foreach loop below.

My question is, since I am not that clear on how arrays and tables work together, how would I rewrite this script to work with the database the same way?

I should also mention that I have already made a table in MySQL with the rows "id" "name" and "url". Any clarification will help.

<?php
require_once('php/autoloader.php');
$feed = new SimplePie();
// Create a new instance of SimplePie
// Load the feeds
$urls = array(
  'http://abcfamily.go.com/service/feed?id=774372' => 'abc',
  'http://www.insideaolvideo.com/rss.xml' => 'aolvideo',
  'http://feeds.bbci.co.uk/news/world/rss.xml' => 'bbcwn',
  'http://www.bing.com' => 'bing',
  'http://www.bravotv.com' => 'bravo',
  'http://www.cartoonnetwork.com' => 'cartoonnetwork',
  'http://feeds.cbsnews.com/CBSNewsMain?format=xml' => 'cbsnews',
  'http://www.clicker.com/' => 'clicker',
  'http://feeds.feedburner.com/cnet/NnTv?tag=contentBody.1' => 'cnet',
  'http://www.comedycentral.com/' => 'comedycentral',
  'http://www.crackle.com/' => 'crackle',
  'http://www.cwtv.com/feed/episodes/xml' => 'cw',
  'http://disney.go.com/disneyxd/' => 'disneyxd',
  'http://www.engadget.com/rss.xml' => 'engadget',
  'http://syndication.eonline.com/syndication/feeds/rssfeeds/video/index.xml' => 'eonline',
  'http://sports.espn.go.com/espn/rss/news' => 'espn',
  'http://facebook.com' => 'facebook',
  'http://flickr.com/espn/rss/news' => 'flickr',
  'http://www.fxnetworks.com//home/tonight_rss.php' => 'fxnetworks',
  'http://www.hgtv.com/' => 'hgtv',
  'http://www.history.com/this-day-in-history/rss' => 'history',
  'http://rss.hulu.com/HuluRecentlyAddedVideos?format=xml' => 'hulu',
  'http://rss.imdb.com/daily/born/' => 'imdb',
  'http://www.metacafe.com/' => 'metacafe',
  'http://feeds.feedburner.com/Monkeyseecom-NewestVideos?format=xml' => 'monkeysee',
  'http://pheedo.msnbc.msn.com/id/18424824/device/rss/' => 'msnbc',
  'http://www.nationalgeographic.com/' => 'nationalgeographic',
  'http://dvd.netflix.com/NewReleasesRSS' => 'netflix',
  'http://feeds.nytimes.com/nyt/rss/HomePage' => 'newyorktimes',
  'http://www.nick.com/' => 'nickelodeon',
  'http://www.nickjr.com/' => 'nickjr',
  'http://www.pandora.com/' => 'pandora',
  'http://www.pbskids.com/' => 'pbskids',
  'http://www.photobucket.com/' => 'photobucket',
  'http://feeds.reuters.com/Reuters/worldNews' => 'reuters',
  'http://www.revision3.com/' => 'revision3',
  'http://www.tbs.com/' => 'tbs',
  'http://www.theverge.com/rss/index.xml' => 'theverge',
  'http://www.tntdrama.com/' => 'tnt',
  'http://www.tvland.com/' => 'tvland',
  'http://www.vimeo.com/' => 'vimeo',
  'http://www.vudu.com/' => 'vudu',
  'http://feeds.wired.com/wired/index?format=xml' => 'wired',
  'http://www.xfinitytv.com/' => 'xfinitytv',
  'http://www.youtube.com/topic/4qRk91tndwg/most-popular#feed' => 'youtube',
);
$feed->set_feed_url(array_keys($urls));
$feed->enable_cache(true);
$feed->set_cache_location('cache');
$feed->set_cache_duration(1800); // Set the cache time
$feed->set_item_limit(1);
$success = $feed->init(); // Initialize SimplePie
$feed->handle_content_type(); // Take care of the character encoding
?>
<?php require_once("inc/connection.php"); ?>
<?php require_once("inc/functions.php"); ?>
<?php include("inc/header.php"); ?>
<?php
// Sort it
$feed_items = array();
// $feed_items is an array
$items = $feed->get_items();
//$items is everything that $items = $feed->get_items(); produces
$urls = array_unique($urls);
// $url = is an empty $
foreach ($urls as $url => $image) {
  $unset = array();
  $feed_items[$url] = array();
  foreach ($items as $i => $item) {
    if ($item->get_feed()->feed_url == $url) {
      $feed_items[$url][] = $item;
      $unset[] = $i;
    }
  }
  foreach ($unset as $i) {
    unset($items[$i]);
  }
}
foreach ($feed_items as $feed_url => $items) {
  if (empty($items)) { ?>
  <div class="item element" data-symbol="<?php echo $urls[$feed_url] ?>" name="<?php echo $urls[$feed_url] ?>">
  <div class="minimise"><img src="images/boreds/<?php echo $urls[$feed_url] ?>.png"/>
  <div class="minimise2">
    <a href="<?php echo $feed_url; ?>"><h2>Visit <?php echo $urls[$feed_url] ?> now!</h2></a>
  </div></div>
  <div class="maximise">
    <a href="<?php echo $feed_url; ?>"><h2>Visit <?php echo $urls[$feed_url] ?> now!</h2></a>
  </div></div>

  <?
    continue;
  }
  $first_item = $items[0];
  $feed = $first_item->get_feed();
  ?>

  <?php

$feedCount = 0;
foreach ($items as $item) {
  $feedCount++;
  ?>
<div class="item element" " data-symbol="<?php echo $urls[$feed_url] ?>" name="<?php echo $urls[$feed_url] ?>">
<div class="minimise"><strong id="amount"><?php echo ''.$feedCount; ?></strong>
  <img src="images/boreds/<?php echo $urls[$feed_url] ?>.png"/>
  <div class="minimise2"><a href="<?php echo $item->get_permalink(); ?>">
  <h2><?php echo $item->get_title(); ?></h2></a>
</div></div>
<div class="maximise"><a href="<?php echo $item->get_permalink(); ?>">
   <h2><?php echo $item->get_title(); ?></h2></a><br><p><?php echo $item->get_description(); ?></p>
</div></div>
<?php
  }
}
?>
<?php require("inc/footer2.php"); ?>

Solution

  • I use SimplePie within some projects and this is what I do to reduce de page load time: I cache the results.

    The flow is, approx., like this:

    1. I put the PHP code that fetch the feeds and produces the HTML in one place. This script is basically equivalent to yours. Say I rename that script to feed-fetcher.php
    2. In this script, I add a feature that saves the generated HTML into something like index.html in the same folder. Indeed, in the root folder where I want to access the feed items content for easy (or future) reading.
    3. I set up a cron job that fetches the feed every 20 minutes or so.
    4. When I want to read the fetched content, I go to the URL of the cached content, stored in the .html file mentioned above. Thus, my page load is really fast as I dont need to wait for the fetching, and processing of every single feed I want to check.

    This solution does not require any database. But of course you can still use a database to store the fetched content formated in HTML as a string, or any other type that suits your needs.

    Is this what you are looking for?