Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: RSS Maker
Forum PHP.pl > Forum > Gotowe rozwiązania > Algorytmy, klasy, funkcje
mls
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() winksmiley.jpg

rss.class.php
  1. <?
  2. class RSS {
  3.     var $ver;
  4.     var $items;
  5.     var $channel;
  6.     var $encoding;
  7.     
  8.     function RSS($version = 2, $encoding = 'utf-8') {
  9.         if ($version != 2) $version = 1;
  10.         $this->ver = $version;
  11.         $this->items = array();
  12.         $this->channel = array();
  13.         $this->encoding = $encoding;
  14.     }
  15.     
  16.     function channel($params) {
  17.         $this->channel = $params;
  18.     }
  19.     
  20.     function addItem($params) {
  21.         $this->items[] = $params;
  22.     }
  23.     
  24.     function addErrorItem($title, $url, $author, $description) {
  25.         $this->items[] = array(
  26.             'title' => RSS::pl2utf(htmlspecialchars($title)),
  27.             'link' => $url,
  28.             'guid' => $url,
  29.             'author' => RSS::pl2utf(htmlspecialchars($author)),
  30.             'date' => time(),
  31.             'description' => RSS::pl2utf(htmlspecialchars($description)));
  32.     }
  33.     
  34.     function output() {
  35.         header(&#092;"Content-Type: text/xml\");
  36.         header(&#092;"Cache-Control: no-cache\");
  37.         echo(&#092;"<?xml version=\"1.0\" encoding=\"\".$this->encoding.\"\"?>n\");
  38.         if ($this->ver == 2) {
  39.             echo(\"<rss version=\"2.0\">n\");
  40.             echo(\"t<channel>n\");
  41.             echo(\"tt<title>\".$this->channel['title'].\"</title>n\");
  42.             echo(\"tt<link>\".$this->channel['link'].\"</link>n\");
  43.             echo(\"tt<description>\".$this->channel['description'].\"</description>n\");
  44.             echo(\"tt<generator>\".$this->channel['generator'].\"</generator>n\");
  45.             echo(\"tt<lastBuildDate>\".date(\"r\").\"</lastBuildDate>n\");
  46.             foreach ($this->items as $item) {
  47.                 echo(\"tt<item>n\");
  48.                 echo(\"ttt<title>\".RSS::rment($item['title']).\"</title>n\");
  49.                 echo(\"ttt<link>\".$item['link'].\"</link>n\");
  50.                 echo(\"ttt<guid>\".$item['guid'].\"</guid>n\");
  51.                 echo(\"ttt<author>\".RSS::rment($item['author']).\"</author>n\");
  52.                 echo(\"ttt<pubDate>\".date(\"r\", $item['date']).\"</pubDate>n\");
  53.                 if ($item['description'] != '')
  54.                     echo(\"ttt<description>\".$item['description'].\"</description>n\");
  55.                 echo(\"tt</item>n\");
  56.             }
  57.             echo(\"t</channel>n\");
  58.             echo(\"</rss>\");
  59.         } else {
  60.             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\");
  61.             echo(\"t<channel rdf:about=\"\".$this->channel['link'].\"\">n\");
  62.             echo(\"tt<title>\".$this->channel['title'].\"</title>n\");
  63.             echo(\"tt<link>\".$this->channel['link'].\"</link>n\");
  64.             echo(\"tt<description>\".$this->channel['description'].\"</description>n\");
  65.             echo(\"tt<dc:publisher>\".$this->channel['generator'].\"</dc:publisher>n\");
  66.             echo(\"tt<items>n\");
  67.             echo(\"ttt<rdf:Seq>n\");
  68.             foreach ($this->items as $item) {
  69.                 echo(\"tttt<rdf:li resource=\"\".$item['guid'].\"\" />n\");
  70.             }
  71.             echo(\"ttt</rdf:Seq>n\");
  72.             echo(\"tt</items>n\");
  73.             echo(\"t</channel>n\");
  74.             foreach ($this->items as $item) {
  75.                 echo(\"t<item rdf:about=\"\".$item['guid'].\"\">n\");
  76.                 echo(\"tt<title>\".RSS::rment($item['title']).\"</title>n\");
  77.                 echo(\"tt<link>\".$item['link'].\"</link>n\");
  78.                 echo(\"tt<dc:date>\".date(\"Y-m-dTH:i:sZ\", $item['date']).\"</dc:date>n\");
  79.                 if ($item['description'] != '')
  80.                     echo(\"tt<description>\".$item['description'].\"</description>n\");
  81.                 echo(\"t</item>n\");
  82.             }
  83.             echo(\"</rdf:RDF>\");
  84.         }
  85.     }
  86.     
  87.     function rment($text) {
  88.         $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  89.         $trans_tbl = array_flip($trans_tbl);
  90.         if (isset($trans_tbl['&lt;'])) unset($trans_tbl['&lt;']);
  91.         if (isset($trans_tbl['&gt;'])) unset($trans_tbl['&gt;']);
  92.         if (isset($trans_tbl['&amp;'])) unset($trans_tbl['&amp;']);
  93.         if (isset($trans_tbl['&apos;'])) unset($trans_tbl['&apos;']);
  94.         if (isset($trans_tbl['&quot;'])) unset($trans_tbl['&quot;']);
  95.         return strtr($text, $trans_tbl);
  96.     }
  97.     
  98.     function pl2utf($tsource) {
  99.         $pl2utf = array(\"&plusmn;\" => \"ą\", \"?\" => \"ą\", \"ć\" => \"ć\", \"ę\" => \"ę\", \"ł\" => \"ł\", \"ń\" => \"ń\", \"ó\" => \"ó\", \"&para;\" => \"ś\", \"?\" => \"ś\", \"ż\" => \"ż\", \"Ľ\" => \"ź\", \"Ą\" => \"ź\", \"ˇ\" => \"Ą\", \"&middot;\" => \"Ą\", \"Ć\" => \"Ć\", \"\" => \"\", \"Ł\" => \"Ł\", \"Ń\" => \"Ń\", \"Ó\" => \"Ó\", \"&brvbar;\" => \"Ś\", \"?\" => \"Ś\", \"Ż\" => \"Ż\", \"&not;\" => \"Ź\", \"?\" => \"Ź\");
  100.         return strtr($tsource, $pl2utf);
  101.     }
  102. }
  103. ?>


Użycie wygląda np. tak:

syndication.php
  1. <?
  2. require_once('rss.class.php');
  3. $rss = new RSS();
  4.  
  5. $channel = array(
  6.   'title' => 'Tytuł',
  7.   'link' => 'http://www.adres.pl/',
  8.   'description' => 'Opis',
  9.   'generator' => 'rss.class.php');
  10. $item = array(
  11.   'title' => 'Tytuł wpisu',
  12.   'link' => 'http://link.do.wpisu/',
  13.   'guid' => 'guid',
  14.   'author' => 'Autor',
  15.   'date' => 'Data',
  16.   'description' => 'Opis');
  17.  
  18. $rss->addItem($item);        
  19. $rss->channel($channel);
  20. $rss->output();
  21. ?>


Całość została napisana na potrzeby jednego z serwisów internetowych (blogi winksmiley.jpg), 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ć winksmiley.jpg
wassago
wydzielony z http://forum.php.pl/index.php?showtopic=21782 - ostatni punkt regulamiu tego forum prosze przeczytac.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.