Prosta klasa do obsługi czytnika RSS z cachowaniem. Klasa do działania wymaga PHP > 5.
Kod
<?php
class Rss {
public $rssFile;
public $cacheFile;
public $xml;
private $cacheDir;
public function __construct ( $rssFile , $cache = 'on' , $cacheDir = 'cache' )
{
$this->rssFile = $rssFile;
$this->cacheFile = basename ( $this->rssFile );
$this->cacheDir = $cacheDir;
if ( empty ( $this->rssFile ) OR empty( $cacheDir ) ) exit;
if ( $cache == 'on' ) {
if ( file_exists ( $this->cacheDir . '/' . $this->cacheFile . '.php' ) ) {
$this->xml = simplexml_load_file ( 'cache/' . $this->cacheFile . '.php' );
} else {
$this->xml = simplexml_load_file ( $this->rssFile );
$cacheFileCode = file_get_contents ( $this->rssFile );
$fp = fopen ( $this->cacheDir . '/' . $this->cacheFile . '.php' , 'w' );
fwrite ( $fp , $cacheFileCode );
fclose ( $fp );
}
} else {
$this->xml = simplexml_load_file ( $this->rssFile );
}
}
public function clearAllCache ()
{
foreach ( glob($this->cacheDir . "/*.php") as $fileCacheName ) {
unlink ( $fileCacheName );
}
}
public function clearOneCache ( )
{
unlink ( $this->cacheDir . '/' . $this->cacheFile . '.php' );
}
public function rssInfo ()
{
$arr['version'] = $this->xml->attributes();
$arr['title'] = $this->xml->channel->title;
$arr['description'] = $this->xml->channel->description;
return $arr;
}
public function getRss()
{
return $this->xml->channel->item;
}
}
?>
class Rss {
public $rssFile;
public $cacheFile;
public $xml;
private $cacheDir;
public function __construct ( $rssFile , $cache = 'on' , $cacheDir = 'cache' )
{
$this->rssFile = $rssFile;
$this->cacheFile = basename ( $this->rssFile );
$this->cacheDir = $cacheDir;
if ( empty ( $this->rssFile ) OR empty( $cacheDir ) ) exit;
if ( $cache == 'on' ) {
if ( file_exists ( $this->cacheDir . '/' . $this->cacheFile . '.php' ) ) {
$this->xml = simplexml_load_file ( 'cache/' . $this->cacheFile . '.php' );
} else {
$this->xml = simplexml_load_file ( $this->rssFile );
$cacheFileCode = file_get_contents ( $this->rssFile );
$fp = fopen ( $this->cacheDir . '/' . $this->cacheFile . '.php' , 'w' );
fwrite ( $fp , $cacheFileCode );
fclose ( $fp );
}
} else {
$this->xml = simplexml_load_file ( $this->rssFile );
}
}
public function clearAllCache ()
{
foreach ( glob($this->cacheDir . "/*.php") as $fileCacheName ) {
unlink ( $fileCacheName );
}
}
public function clearOneCache ( )
{
unlink ( $this->cacheDir . '/' . $this->cacheFile . '.php' );
}
public function rssInfo ()
{
$arr['version'] = $this->xml->attributes();
$arr['title'] = $this->xml->channel->title;
$arr['description'] = $this->xml->channel->description;
return $arr;
}
public function getRss()
{
return $this->xml->channel->item;
}
}
?>
Uzywanie
Kod
$rss = new Rss ( 'adres do pliku xml' , 'on/off cache', 'katalog dla cache' );
ps. Aby cache było włączone musimy dać on
Wyświetlanie informacji o kanale.
Kod
$info = $rss->rssInfo();
echo 'Wersja RSS = ' . $info['version'] . '<br />';
echo 'Tytuł kanału = ' . $info['title'] . '<br />';
echo 'Opis kanalu = ' . $info['description'] . '<br />';
echo 'Wersja RSS = ' . $info['version'] . '<br />';
echo 'Tytuł kanału = ' . $info['title'] . '<br />';
echo 'Opis kanalu = ' . $info['description'] . '<br />';
Wyświetlenie notek
Kod
foreach ( $rss->getRss() as $item )
{
echo '<h1>' . $item->title . ' (' . $item->pubDate . ')</h1><br /><small>' . $item->description . '</small><br />' . $item->link;
}
{
echo '<h1>' . $item->title . ' (' . $item->pubDate . ')</h1><br /><small>' . $item->description . '</small><br />' . $item->link;
}
Aby wyczyścić ten cache którego aktualnie używamy dajemy
Kod
$rss->ClearOneCache();
a żeby wyczyścić wszystkie cache dajemy
Kod
$rss->ClearAllCache();
Może komuś się przyda

ps. Użyłem znaczników code a nie php bo php coś szwankuje.