Dynamické generování RSS pomocí PHP
- Vložil Trupík 2/14/2007 10:29:38 AM
-
RSS je nástroj, jak vaše čtenáře upozornit na nový obsah stránek.
Jedná se o jednoduchý dialekt XML, který zpracovávají čtečky RSS.
V tomto článku ukážu třídu, pomocí které lze RSS velmi jednoduše
generovat.
Třída opět používá můj MyXmlWriter.
Vlastní použití třídy vypadá takto:
<?
require_once("RssGenerator.class.php");
$rss_gen = new RssGenerator();
//nastavení vlastností RSS kanálu
$rss_gen->title = "Example.com - články";
$rss_gen->link = "http://www.example.com";
$rss_gen->description = "Nové články;
// vybrani vsech clanku z tabulky, konstrukt AS odpovida jednotlivym poduzlum
// jednoho uzlu <item>
$sql =
"SELECT nazev AS title, CONCAT('http://www.example.com/clanky/', id) AS link, popis AS description FROM clanky";
$res = mysql_query($sql);
$rss_gen->Generate("rss.xml", $res);
?>
Vlastní třída vypadá takto:
<?
require_once("MyXmlWriter.class.php");
class RssGenerator
{
private $_writer;
/* channel properties */
public $title;
public $link;
public $description;
public $language;
public $pubDate;
public $lastBuildDate;
public $managingEditor;
public $webMaster;
public $copyright;
public $category;
// time to live
public $ttl;
public $image_url;
private function ifExistsThenWrite($par)
{
if ($this->$par != null && $this->$par != "")
{
$this->_writer->WriteBeginTag($par);
$this->_writer->WriteText($this->$par);
$this->_writer->CloseTag();
}
}
/**
* Zapis rss do souboru
* V SQL dotaze pouzit AS konstrukci pro vyznamne sloupce:
* povinne: title, link description
* nepovinne: pubDate, guid(string), author,
* category, coments, source
*
* @param string $filename
* @param mysql_resource $res
*/
public function Generate($filename, $res)
{
$w = new MyXmlWriter($filename);
$this->_writer = $w;
$w->htmlentities = 0;
$w->WriteXMLDeclaration("1.0", "windows-1250");
$w->WriteBeginTag("rss");
$w->WriteAttribute("version", "2.0");
$w->WriteBeginTag("channel");
/* genereta channel subitems */
$this->ifExistsThenWrite("title");
$this->ifExistsThenWrite("link");
$this->ifExistsThenWrite("description");
$this->ifExistsThenWrite("language");
$this->ifExistsThenWrite("pubDate");
$this->ifExistsThenWrite("lastBuildDate");
$this->ifExistsThenWrite("managingEditor");
$this->ifExistsThenWrite("webMaster");
$this->ifExistsThenWrite("copyright");
$this->ifExistsThenWrite("category");
$this->ifExistsThenWrite("ttl");
if ($this->image_url != null && $this->image_url != "")
{
$w->WriteBeginTag("image");
$w->WriteBeginTag("url");
$w->WriteText($this->image_url);
$w->CloseTag();
$w->WriteBeginTag("link");
$w->WriteText($this->link);
$w->CloseTag();
$w->WriteBeginTag("title");
$w->WriteText($this->title);
$w->CloseTag();
$w->CloseTag(); //image
}
while ($a = mysql_fetch_assoc($res))
{
$w->WriteBeginTag("item");
$w->WriteBeginTag("title");
$w->WriteText($a["title"]);
$w->CloseTag();
$w->WriteBeginTag("link");
$w->WriteText($a["link"]);
$w->CloseTag();
$w->WriteBeginTag("description");
$w->WriteText($a["description"]);;
$w->CloseTag();
$this->quickWriteAtribute($w, "guid", $a);
$this->quickWriteAtribute($w, "author", $a);
$this->quickWriteAtribute($w, "category", $a);
$this->quickWriteAtribute($w, "coments", $a);
$this->quickWriteAtribute($w, "source", $a);
$w->CloseTag(); //item
}
$this->_writer->CloseTag();
$this->_writer->CloseAllTags();
}
private function quickWriteAtribute($w, $nodeName, $array)
{
if (array_key_exists($nodeName, $array))
{
$w->WriteBeginTag($nodeName);
$w->WriteText($array[$nodeName]);
$w->CloseTag();
}
}
}
?>
Ohodnoťte prosím užitečnost článku