PHP: Lezione 7 – RSS and Atom
Posted on giugno 21, 2008
Filed Under Appunti di Php | Leave a Comment
I Feed Web sono il meccanismo attraverso il quale i contenuti vengono distribuiti tramite Web nel formato XML. L’accesso ai Feed avviene solitamente tramite degli aggregatori che permettono di accedere ai contenuti dei vari siti senza collegarsi direttamente ad essi ogni volta.
Ad oggi la maggior parte della piattaforme di blogging includono un feed e lo stesso i CMS. Le scelte solitamente ricadono su RSS ed Atom.
Entrambi si basano sull’XML e possono essere visti come file di testo ma non ha senso leggerli come tali. Occorre un lettore per interpretare lo schema. RSS e’ conosciuto in due branche: RDF Site Summary (RSS 0.9) creato da Netscape alla fine degli anni novanta e RSS 0.91 meno complessa. In seguito RSS 1.0 e’ diretta discendente di RSS 0.9 mentre RSS 2.0 e’ vicina a RSS 0.91. Ad oggi RSS 2.0 sta per Really Simple Syndication e RSS 1.0 sta per RDF Site Summary. Maggiori informazioni su RSS 2.0 sono disponibili:
http://blogs.law.harvard.edu/tech/rss
Atom e’ nato all’inizio del 2003 in due versioni: Atom 0.3 e Atom 1.0. Entrambe sono molto complicate e raramente usate. Un confronto fra RSS ed Atom e’ disponibile qui:
http://www.intertwingly.net/wiki/pie/Rss20AndAtom10Compared
Ecco un esempio di RSS 2.0:
<rss version=”2.0”>
<channel>
<title>example.com – Esempi</title>
<link>http://www.example.com</link>
<description>Descrizione del feed</description>
<language>it</language>
<pubDate>Mon, 10 Feb 2008 18:56:23 EDT</pubDate>
<item>
<title>Titolo</title>
<link>http://www.example.com/titolo.html</link>
<description>La descrizione valida.</description>
<pubDate>Mon, 10 Feb 2008 18:56:23 EDT</pubDate>
</item>
<item>
<title>Un altro titolo</title>
<link>http://www.example.com/titolo2.html</link>
<description>La seconda descrizione.</description>
<pubDate>Mon, 10 Feb 2008 18:56:23 EDT</pubDate>
</item>
</channel>
</rss>
Il feed puo’ contenere uno svariato numero di elementi <item>.Nel seguito una classe per la generazione degli RSS all’interno della directory include chiamato rss_factory.inc.php:
<?php
class RSSFactory
{
var $_title;
var $_link;
var $_description;
var $_language;
var $_items;
// escape string characters for inclusion in XML structure
function _escapeXML($str)
{
$translation = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($translation as $key => $value)
{
$translation[$key] = ‘&#‘ . ord($key) . ‘;’;
}
$translation[chr(38)] = ‘&‘;
return preg_replace(“/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/“,”&”,
strtr($str, $translation));
}
// the class constructor is executed when creating an instance of the class
function RSSFactory($title, $link, $description,
$language = ‘en-us’, $items = array())
{
// save feed data to local class members
$this->_title = $title;
$this->_link = $link;
$this->_description = $description;
$this->_language = $language;
$this->_items = $items;
}
// adds a new feed item
function addItem($title, $link, $description, $additional_fields = array())
{
// add feed item
$this->_items[] =
array_merge(array(‘title’ => $title,
‘link’ => $link,
‘description’ => $description),
$additional_fields);
}
// generates feed
function get()
{
// initial preparations
ob_start();
header(‘Content-type: text/xml’);
// generate feed header
echo ‘<rss version=”2.0”>’ .
‘<channel>’ .
‘<title>’ . RSSFactory::_escapeXML($this->_title) . ‘</title>’ .
‘<link>’ . RSSFactory::_escapeXML($this->_link) . ‘</link>’ .
‘<description>’ .
RSSFactory::_escapeXML($this->_description) .
‘</description>’;
// add feed items
foreach ($this->_items as $feed_item)
{
// add a feed item and its contents
echo ‘<item>’;
foreach ($feed_item as $item_name => $item_value)
{
echo “<$item_name>” .
RSSFactory::_escapeXML($item_value) .
“</$item_name>”;
}
echo ‘</item>’;
}
// close channel and rss elements
echo ‘</channel></rss>’;
// return feed data
return ob_get_clean();
}
}
?>
Creiamo il file feed.php cosi’ fatto:
<?php
// load the URL factory library
require_once ‘include/rss_factory.inc.php’;
// create feed
$rss_feed = new RSSFactory(‘SEOEgghead.com New Products Feed’, ‘http://www.seoegghead.com/seo-with-php-updates.html’, ‘Exciting new products, updated daily’);
// add feed item
$rss_feed->addItem(‘New Link Juice with Orange Flavor!’, ‘http://seophp.example.com/Products/SEO-Toolbox-C6/Link-Juice-P31.html’, ‘The new Link Juice product of SEOEgghead.com can do wonders for your website!’);
// add feed item
$rss_feed->addItem(‘Enhance Your PHP Applications with AJAX!’, ‘http://seophp.example.com/Products/Friends-Shed-C2/AJAX-PHP-Book-P42.html’, ‘Check out this AJAX tutorial for PHP developers!’);
// display feed
echo $rss_feed->get();
?>
Ringraziamo Skyzyx Technologies per lo sviluppo di una libreria chiamata SimplePie (http://simplepie.org/) che permette di utilizzare una classe scritta in PHP per leggere RSS e Atom mantenendo facile l’utilizzo.
Di seguito un esempio di utilizzo della classe per leggere gli RSS creati:
- Abilitare le Curl dal file php.ini:
extension=php_curl.dll
- Scarichiamo SimplePie da http://simplepie.org/downloads/.
- Scompattare il file e salvare il file simplepie.inc nella directory include.
- Testiamo la compatibilita’ del sistema avviando lo script: sp_compatiliby_test.php.
- creiamo una directory chiamata cache che sara’ utilizzata da simplePie per la cache dei dati.
- Creiamo il file readFeed.php cosi’ fatto:
<?php
// load the SimplePie library
require_once ‘include/simplepie.inc’;
// create and configure SimplePie object
$feed = new SimplePie();
$feed->feed_url(‘http://seophp.example.com/feed.php’);
$feed->cache_location(‘cache’);
$feed->init();
$feed->handle_content_type();
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html>
<head>
<title>Feed Reading Test</title>
</head>
<body>
<?php
if ($feed->data)
{
// display the title
echo ‘<h1>’ .
‘<a href=”‘ . $feed->get_feed_link() . ‘“>’ .
$feed->get_feed_title() .
‘</a>’ .
‘</h1>’;
// display a maximum of 5 feed items
$max = $feed->get_item_quantity(5);
for ($x=0; $x<$max; $x++)
{
$item = $feed->get_item($x);
// display feed link and title
echo ‘<h2>’ .
‘<a href=”‘ . $item->get_permalink() . ‘“>’ .
$item->get_title() .
‘</a>’ .
‘</h2>’;
// display feed description
echo ‘<p>’ . $item->get_description() . ‘</p>’;
}
}
?>
</body>
</html>
- Caridhiamo il file readFeed.php ed avremo i feed creati.
E’ anche possibile leggere i dati da un feed esterno in questo modo:
<?php
// load the SimplePie library
require_once ‘include/simplepie.inc’;
// create and configure SimplePie object
$feed = new SimplePie();
$feed->feed_url(‘http://seophp.example.com/feed.php’);
$feed->cache_location(‘cache’);
$feed->init();
$feed->handle_content_type();
?>
Others Script adv
Leave a Reply