SimplePortal

Customization => Custom Coding => Topic started by: wille on December 07, 2011, 11:28:20 AM

Title: [BLOCK] Custom RSS reader with caching
Post by: wille on December 07, 2011, 11:28:20 AM
Here's a simple RSS reader using SimpleXML. Features caching. In this example code I decided to parse a feed one user here had problems parsing with the built-in RSS reader.

*NOTE: You must create a folder cache/rss and make it writable.

Using this code requires a bit of php knowledge. If you want to read a feed that uses different kind of formatting, you have to manipulate the code a bit. I.e. change $xml->entry to $xml->item, $item->summary to $item->description etc.

Modify this code as you want. Comes with no warranty, whatsoever :-)

Code: [Select]
<?php
//Configuration
$url="http://www.swtor.com/feed/news/all";
$items="2";
// Load the cache file if it exists
$hashfromURL hash("md5",$url);
$cachefile "cache/rss/".$hashfromURL.".html";
$cachetime 60*60// Cache refresh time in seconds. Default is one hour.
if(file_exists($cachefile) && (time() - filemtime($cachefile) < $cachetime )) {
include($cachefile);
}
else {
// Try to load the RSS. If it's not possible, load the cached version instead.
$xml simplexml_load_file($url);
if ((!$xml) && file_exists($cachefile)) include($cachefile);
if ((!$xml) && !(file_exists($cachefile))) echo 'Cannot load feed.';
if ($xml){
ob_start();
$i=0;
foreach ($xml->entry as $item){
// If you're using unicode forum, remove the utf8_decode() function
// Look into the XML file you're reading and check which tags are used for which info.
echo ' <a href="'.htmlspecialchars($item->id).'">'.
utf8_decode($item->title).'</a><br />'.
utf8_decode($item->summary).'<br />Published: '.
substr($item->updated,0,10).'<br />';

$i++;
if ($i==$items) break;
}
// Write the contents into the cache file
$fp fopen($cachefile'w');
fwrite($fpob_get_contents());
fclose($fp);
ob_end_flush(); 
        }
}
// To parse namespace tags, eg. <dc:date></dc:date>:
//  $dc = $item->children($namespaces['dc']);
// $date = $dc->date;
// To parse tags like <gd:when startTime='2011-03-09'/> such as in google calender events feed use (after fetching 
//  $gd = $item->children($namespaces['gd']);
//  $gd->when->attributes()->startTime
?>
Title: Re: [BLOCK] Custom RSS reader with caching
Post by: Zirc on December 07, 2011, 05:51:44 PM
This works great Willie, thanks.  One question... how would I format the date so that it is M-D-Y instead of Y-M-D?
Title: Re: [BLOCK] Custom RSS reader with caching
Post by: wille on December 07, 2011, 05:59:45 PM
Add line
Code: [Select]
$newdate = date("m-d-Y", strtotime($item->updated));And then "Published: '.substr($newdate,0,10).'<br />';"
Title: Re: [BLOCK] Custom RSS reader with caching
Post by: Zirc on December 07, 2011, 06:10:59 PM
The date is in the correct format now but all the items are showing 12-31-1969
Title: Re: [BLOCK] Custom RSS reader with caching
Post by: Zirc on December 08, 2011, 12:08:21 AM
I'm also getting a couple errors with this block now:

http://www._____.com/index.php?action=admin;area=portalblocks;sa=edit;block_id=55;b47eb0bef=b8803de73649894fd50a88995a9bbfe4
8: Trying to get property of non-object
File: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/forums/Sources/PortalBlocks.php(3562) : eval()'d code
Line: 5

http://www._____.com/index.php?action=admin;area=portalblocks;sa=edit;block_id=55;b47eb0bef=b8803de73649894fd50a88995a9bbfe4
8: Undefined variable: item
File: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/forums/Sources/PortalBlocks.php(3562) : eval()'d code
Line: 5
Title: Re: [BLOCK] Custom RSS reader with caching
Post by: wille on December 08, 2011, 04:03:03 AM
Try this:
Code: [Select]
<?php
//Configuration
$url="http://www.swtor.com/feed/news/all";
$items="2";
// Load the cache file if it exists
$hashfromURL hash("md5",$url);
$cachefile "cache/rss/".$hashfromURL.".html";
$cachetime 60*60// Cache refresh time in seconds. Default is one hour.
if(file_exists($cachefile) && (time() - filemtime($cachefile) < $cachetime )) {
include($cachefile);
}
else {
// Try to load the RSS. If it's not possible, load the cached version instead.
$xml simplexml_load_file($url);
if ((!$xml) && file_exists($cachefile)) include($cachefile);
if ((!$xml) && !(file_exists($cachefile))) echo 'Cannot load feed.';
if ($xml){
ob_start();
$i=0;
foreach ($xml->entry as $item){
// If you're using unicode forum, remove the utf8_decode() function
// Look into the XML file you're reading and check which tags are used for which info.
$newdate date("m-d-Y"strtotime($item->updated));
echo ' <a href="'.htmlspecialchars($item->id).'">'.
utf8_decode($item->title).'</a><br />'.
utf8_decode($item->summary).'<br />Published: '.
substr($newdate,0,10).'<br />';

$i++;
if ($i==$items) break;
}
// Write the contents into the cache file
$fp fopen($cachefile'w');
fwrite($fpob_get_contents());
fclose($fp);
ob_end_flush(); 
        }
}
// To parse namespace tags, eg. <dc:date></dc:date>:
//  $dc = $item->children($namespaces['dc']);
// $date = $dc->date;
// To parse tags like <gd:when startTime='2011-03-09'/> such as in google calender events feed use (after fetching 
//  $gd = $item->children($namespaces['gd']);
//  $gd->when->attributes()->startTime
?>

You have to wait for the cache to refresh or set the refresh interval to zero.

As I said, it's gonna take a little php knowledge to use this script. :-)
Title: Re: [BLOCK] Custom RSS reader with caching
Post by: Zirc on December 08, 2011, 05:44:27 PM
I have some PHP knowledge but not a lot.  Since it was throwing errors I figured I'd post here just incase anybody else needed to use the code.

Thanks, the new code works perfectly now.

 :applause:
Title: Re: [BLOCK] Custom RSS reader with caching
Post by: ccbtimewiz on December 09, 2011, 12:43:15 AM
You might be interested in integrating with the cache system of SMF 2.0.

You can use functions such as cache_put_data and cache_get_data, which will automatically (de)serialize the cache data.

You can use the Function DB on SM.org for more information on how to use the functions if you're unaware.

SimplePortal 2.3.8 © 2008-2024, SimplePortal