Jak Wy, to i ja... Moja prosta klasa do RSSów. Funkcja do zmiany pliterek użyta dla mojej własnej wygody, by nie korzystać z iconv()
rss.class.php<?
class RSS {
var $ver;
var $items;
var $channel;
var $encoding;
function RSS($version = 2, $encoding = 'utf-8') {
if ($version != 2) $version = 1;
$this->ver = $version;
$this->channel = array(); $this->encoding = $encoding;
}
function channel($params) {
$this->channel = $params;
}
function addItem($params) {
$this->items[] = $params;
}
function addErrorItem($title, $url, $author, $description) {
'link' => $url,
'guid' => $url,
}
function output() {
header(\"Content-Type: text/xml\"); header(\"Cache-Control: no-cache\"); echo(\"<?xml version=\"1.0\" encoding=\"\".$this->encoding.\"\"?>n\");
if ($this->ver == 2) {
echo(\"<rss version=\"2.0\">n\");
echo(\"t<channel>n\");
echo(\"tt<title>\".$this->channel['title'].\"</title>n\");
echo(\"tt<link>\".$this->channel['link'].\"</link>n\");
echo(\"tt<description>\".$this->channel['description'].\"</description>n\");
echo(\"tt<generator>\".$this->channel['generator'].\"</generator>n\");
echo(\"tt<lastBuildDate>\".date(\"r\").\"</lastBuildDate>n\");
foreach ($this->items as $item) {
echo(\"tt<item>n\");
echo(\"ttt<title>\".RSS::rment($item['title']).\"</title>n\");
echo(\"ttt<link>\".$item['link'].\"</link>n\");
echo(\"ttt<guid>\".$item['guid'].\"</guid>n\");
echo(\"ttt<author>\".RSS::rment($item['author']).\"</author>n\");
echo(\"ttt<pubDate>\".date(\"r\", $item['date']).\"</pubDate>n\");
if ($item['description'] != '')
echo(\"ttt<description>\".$item['description'].\"</description>n\");
echo(\"tt</item>n\");
}
echo(\"t</channel>n\");
echo(\"</rss>\");
} else {
echo(\"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns=\"http://purl.org/rss/1.0/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">n\");
echo(\"t<channel rdf:about=\"\".$this->channel['link'].\"\">n\");
echo(\"tt<title>\".$this->channel['title'].\"</title>n\");
echo(\"tt<link>\".$this->channel['link'].\"</link>n\");
echo(\"tt<description>\".$this->channel['description'].\"</description>n\");
echo(\"tt<dc:publisher>\".$this->channel['generator'].\"</dc:publisher>n\");
echo(\"tt<items>n\");
echo(\"ttt<rdf:Seq>n\");
foreach ($this->items as $item) {
echo(\"tttt<rdf:li resource=\"\".$item['guid'].\"\" />n\");
}
echo(\"ttt</rdf:Seq>n\");
echo(\"tt</items>n\");
echo(\"t</channel>n\");
foreach ($this->items as $item) {
echo(\"t<item rdf:about=\"\".$item['guid'].\"\">n\");
echo(\"tt<title>\".RSS::rment($item['title']).\"</title>n\");
echo(\"tt<link>\".$item['link'].\"</link>n\");
echo(\"tt<dc:date>\".date(\"Y-m-dTH:i:sZ\", $item['date']).\"</dc:date>n\");
if ($item['description'] != '')
echo(\"tt<description>\".$item['description'].\"</description>n\");
echo(\"t</item>n\");
}
echo(\"</rdf:RDF>\");
}
}
function rment($text) {
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
if (isset($trans_tbl['<'])) unset($trans_tbl['<']);
if (isset($trans_tbl['>'])) unset($trans_tbl['>']);
if (isset($trans_tbl['&'])) unset($trans_tbl['&']);
if (isset($trans_tbl['''])) unset($trans_tbl[''']);
if (isset($trans_tbl['"'])) unset($trans_tbl['"']);
return strtr($text, $trans_tbl);
}
function pl2utf($tsource) {
$pl2utf = array(\"±\" => \"ą\", \"?\" => \"ą\", \"ć\" => \"ć\", \"ę\" => \"ę\", \"ł\" => \"ł\", \"ń\" => \"ń\", \"ó\" => \"ó\", \"¶\" => \"ś\", \"?\" => \"ś\", \"ż\" => \"ż\", \"Ľ\" => \"ź\", \"Ą\" => \"ź\", \"ˇ\" => \"Ą\", \"·\" => \"Ą\", \"Ć\" => \"Ć\", \"\" => \"\", \"Ł\" => \"Ł\", \"Ń\" => \"Ń\", \"Ó\" => \"Ó\", \"¦\" => \"Ś\", \"?\" => \"Ś\", \"Ż\" => \"Ż\", \"¬\" => \"Ź\", \"?\" => \"Ź\");
return strtr($tsource, $pl2utf);
}
}
?>
Użycie wygląda np. tak:
syndication.php<?
require_once('rss.class.php');
$rss = new RSS();
'title' => 'Tytuł',
'link' => 'http://www.adres.pl/',
'description' => 'Opis',
'generator' => 'rss.class.php');
'title' => 'Tytuł wpisu',
'link' => 'http://link.do.wpisu/',
'guid' => 'guid',
'author' => 'Autor',
'date' => 'Data',
'description' => 'Opis');
$rss->addItem($item);
$rss->channel($channel);
$rss->output();
?>
Całość została napisana na potrzeby jednego z serwisów internetowych (blogi
), i w działaniu sprawuje się jak należy, choć może nie jest to najciekawszy i specjalnie uniwersalny kod...
Hm, jak widzę funkcja do zmiany pliterek się nieco rozsypała. Trudno. Ogólnie może jej nie być
Ten post edytował mls 15.10.2004, 03:47:37