четверг, 3 мая 2007 г.

Класс на PHP для создания новостной ленты в формате RSS 2.0

На сайте RUSSLAVBANK есть ссылка для получения новостей в формате RSS 2.0.
Для формирования RSS-ленты я написал класс. Так как все мои сайты написаны на PHP, соответственно и данный класс написан на нем же :)
Файл с описанием класса называется ekrss.phpc.
Пример скрипта новостной ленты, с использованием этого класса, выложу позже.



<?
//____________________________________________________________________________
// ekrss.phpc
// RSS Class v1.0.9
// Author: Eugene Klepikov
// E-mail: klek@comtv.ru
// Created: 03.04.2006
// Updated: 29.05.2006
// Copyright: (c) 2006 Klek
// Licence: GPL
//_____________________________________________________________________________
class ekMakeRSS
{
private $xml;
private $rss;
private $Title;
private $Link;
private $Description;
private $Lang;
private $Encoding;
private $pubDate;
private $lastBuildDate;
private $Copyright;
private $Generator;
private $Docs;
private $managingEditor;
private $webMaster;

private $imageTitle;
private $imageURL;
private $imageLink;
private $imageDescription;
private $imageWidth;
private $imageHeight;

private $itemTitle;
private $itemLink;
private $itemDescription;
private $itemAuthor;
private $itemCategory;
private $itemGuid;
private $itemPubDate;
private $itemComments;
private $itemEnclosure;
private $itemSource;

private $Items;
//-----------------------------------------------------------------------------

function __construct()
{
$this->Encoding="windows-1251";
$this->xml="<?xml version=\"1.0\" encoding=\"{$this->Encoding}\" ?>\n";
$this->rss="<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
$this->Title="My RSS Channel";
$this->Link="http://";
$this->Description="My RSS Channel";
$this->Lang="ru";
$this->pubDate="";
$this->lastBuildDate=gmdate("D, d M Y H:i:s")." +0300";
$this->Copyright="Copyright";
$this->Generator="ekRSS v1.0.9 (c)Klek, 2006";
$this->Docs="http://blogs.law.harvard.edu/tech/rss";
$this->managingEditor="";
$this->webMaster="";

$this->imageTitle="";
$this->imageURL="";
$this->imageLink="";
$this->imageDescription="";
$this->imageWidth="";
$this->imageHeight="";

$this->Items=array();
$this->itemTitle="";
$this->itemLink="";
$this->itemDescription="";
$this->itemAuthor="";
$this->itemCategory="";
$this->itemGuid="";
$this->itemPubDate="";
$this->itemComments="";
$this->itemEnclosure="";
$this->itemSource="";
}
//-----------------------------------------------------------------------------

function __set($varName,$varValue)
{
if(isset($this->$varName))
{
$this->$varName=$varValue;
if($varName=="Encoding")
{
$this->xml="<?xml version=\"1.0\" encoding=\"{$this->Encoding}\" ?>\n";
}
}
}
//-----------------------------------------------------------------------------

function __get($varName)
{
return (isset($this->$varName)) ? $this->$varName : $this->$varName="";
}
//-----------------------------------------------------------------------------

function AddItem()
{
$arItem=array();
$arItem['title']=$this->itemTitle;
$arItem['link']=$this->itemLink;
$arItem['description']=$this->itemDescription;
$arItem['author']=$this->itemAuthor;
$arItem['comments']=$this->itemComments;
$arItem['enclosure']=$this->itemEnclosure;
$arItem['guid']=$this->itemGuid;
$arItem['pubDate']=$this->itemPubDate;
$arItem['source']=$this->itemSource;

$this->Items[]=$arItem;
}
//-----------------------------------------------------------------------------

private function __SendItem()
{
foreach($this->Items as $Item)
{
echo "<item>\n";
echo "<title><![CDATA[{$Item['title']}]]></title>\n";
echo "<link>{$Item['link']}</link>\n";
echo "<description><![CDATA[{$Item['description']}]]></description>\n";
if(trim($Item['author']) != "") echo "<dc:creator>{$Item['author']}</dc:creator>\n";
if(trim($Item['category']) != "") echo "<dc:subject>{$Item['category']}</dc:subject>\n";
if(trim($Item['comments']) != "") echo "<comments>{$Item['comments']}</comments>\n";
if(trim($Item['pubDate']) != "") echo "<pubDate>{$Item['pubDate']}</pubDate>\n";
if(trim($Item['guid']) != "") echo "<guid isPermaLink=\"true\">{$Item['guid']}</guid>\n";
if(trim($Item['source']) != "") echo "<source>{$Item['source']}</source>\n";
echo "</item>\n";
}
}
//-----------------------------------------------------------------------------

private function __SendImage()
{
if($this->imageTitle != "" and $this->imageURL != "" and $this->imageLink != "")
{
echo "<image>\n";
echo "<title>{$this->imageTitle}</title>\n";
echo "<url>{$this->imageURL}</url>\n";
echo "<link>{$this->imageLink}</link>\n";
if(trim($this->imageDescription) != "") echo "<description>{$this->imageDescription}</description>\n";
if(trim($this->imageWidth != "")) echo "<width>{$this->imageWidth}</width>\n";
if(trim($this->imageHeight != "")) echo "<height>{$this->imageHeight}</height>\n";
echo "</image>\n";
}
}
//-----------------------------------------------------------------------------
function Send()
{
header("Content-Type: text/xml");
echo $this->xml;
echo $this->rss;
echo "<channel>\n";
echo "<title>{$this->Title}</title>\n";
echo "<link>{$this->Link}</link>\n";
echo "<description>{$this->Description}</description>\n";
echo "<language>{$this->Lang}</language>\n";
echo "<copyright>{$this->Copyright}</copyright>\n";
if(trim($this->pubDate) != "")
echo "<pubDate>{$this->pubDate}</pubDate>\n";
if(trim($this->lastBuildDate) != "")
echo "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\n";
echo "<docs>{$this->Docs}</docs>\n";
echo "<generator>{$this->Generator}</generator>\n";
if(trim($this->managingEditor) != "")
echo "<managingEditor>{$this->managingEditor}</managingEditor>\n";
if(trim($this->webMaster) != "")
echo "<webMaster>{$this->webMaster}</webMaster>\n";
$this->__SendImage();
$this->__SendItem();
echo "</channel>\n";
echo "</rss>\n";
}
//-----------------------------------------------------------------------------
}
?>

Комментариев нет: