RSS and
PHP
If you want to
display your (or other's) news at your website – you will be amazed by
how simple it is.
// caching the
RSS file
// place where to store the cache
file
$filename = $_SERVER['DOCUMENT_ROOT']."/cache/phpnews.rss";
// checking
how old the file is
$modif=time()-@filemtime ("$filename");
// if there is no
file in the cache or it is too old - update it
if(!file_exists($filename) || $modif>"3600")
{
$rss = file_get_contents("http://www.php.net/news.rss");
$handle = fopen ("$filename", "w");
fwrite($handle, $rss);
fclose($handle);
}
// importing the
RSS library from the PEAR package
require_once("XML/RSS.php");
$rss =& new XML_RSS("$filename");
$rss->parse();
foreach ($rss->getItems() as $value) {
// making the news string
$page = $page."<b>".$value['dc:date']."</b><a href=\"".$value['link']."\" class=\"menu\">"." ".$value['title']. "</a><br>" .$value['description'] . "\n";
}
// displaying the result
echo"$page";
This script
demonstrates the basic idea of working with RSS files. If you want to
use a ready-made solution,
Feed2Html is exactly what you need.
See also: RSS
aggregator, RSS feeds,
RSS specifications.
|