Delta RSS
Delta prezentuje funkcję do tworzenia kanałów
RSS. Wystarczy wygenerować dwie tablice: $channel oraz $items
zawierające odpowiednie klucze, dołączyć plik (lub wstawić do kodu poniższy kod)
i wywołać funkcję make_rss(), podając jako parametry w/w tablice.
W moim przykładzie wiele zmiennych jest predefiniowanych - możesz zrobić to samo dla swojego kanału, ale pamiętaj o usunięciu "moich" wartości. Prosiłbym jednak o pozostawienie wpisu <generator> oraz przestrzeganie licencji. Pola zostały określone na podstawie specyfikacji RSS 2.0, jednak nie wszystkie z nich zostały zaimplementowane, przykro mi. Struktura tablicy channel odpowiada głównym polom w kanale (klucz tablicy będzie nazwą pola), z małym wyjątkiem: 'image' powinno wskazywać na URL obrazka (title i link zostaną przypisane automatycznie). $items jest tablicą w której znajdują się elementy, które to z kolei są tablicami asocjacyjnymi: zasada taka sama jak w $channel.
Źródła modułu "karty" można znaleźć na delcie.
<?
function fill( &$s, $v ) { if( empty($s) ) return $s = $v; }
function hsc( $s ){ return ( strpos( $s, '<![CDATA[' ) !== false )
? $s : htmlspecialchars( $s ); }
function to_xml ( &$a ){ foreach( $a as $k => $v ) $a[$k] = hsc( $v ); }
//function to_xml ( &$a ){ foreach( $a as $k => $v ) $a[$k] = '<![CDATA['.$v.']]>'; }
function make_rss($channel, $items){
include_once( 'karty.php' );
header( 'Content-type: application/rss+xml' );
$rss = new karty();
fill( $channel['encoding'], 'ISO-8859-2' );
fill( $channel['ttl'], '180' );
fill( $channel['title'], $channel['tytul'] );
fill( $channel['link'], 'http://'.$_SERVER['HTTP_HOST'] );
fill( $channel['description'], $channel['desc'] );
fill( $channel['webMaster'], 'puck@dx.one.pl (Maciej Łebkowski)' );
fill( $channel['managingEditor'],'puck@dx.one.pl (Maciej Łebkowski)' );
fill( $channel['language'], 'pl' );
fill( $channel['copyright'],
'http://creativecommons.org/licenses/by-sa/2.0/pl/' );
to_xml( $channel );
$rss->DodajDane( $channel );
if( $image['url'] = $channel['image'] ){
$image['title'] = $channel['title'];
$image['link'] = $channel['link'];
$rss->LadujSzablon( 'image', <<<IMAGE
<image_block><image>
<url>&image:url;</url>
<title>&image:title;</title>
<link>&image:link;</link>
</image></image_block>
IMAGE
); $rss->LadujDane( 'image', $image );
}
$custom = '';
foreach( array( 'copyright', 'managingEditor',
'webMaster', 'pubDate' ) as $n )
if( $v = $channel[$n] ) $custom .= "\t<$n>$v</$n>\n";
$rss->LadujDane( 'custom', $custom );
$rss->LadujSzablon( 'rss', <<<EOF
<?xml version="1.0" encoding="&encoding;" ?>
<rss version="2.0">
<channel>
<title>&title;</title>
<link>&link;</link>
<description>&description;</description>
<language>&language;</language>
<ttl>&ttl;</ttl>
&karty:image;
&custom;
<generator>Delta RSS, http://delta.lebkowski.info/rss</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
&karty:items;
</channel>
</rss>
EOF
);
$rss_items = array();
if( is_array($items) ) foreach( $items as $item ){
// fill the missing data...
fill ( $item['pubDate'], $item['data'] );
fill ( $item['description'], $item['desc'] );
if( $item['path'] )
$item['link'] = 'http://'.$_SERVER['HTTP_HOST']
.htmlspecialchars($item['path']);
//format the data
if( is_numeric( $u = $item['pubDate'] ) )
$item['pubDate'] = date('r', $u );
to_xml( $item );
$custom = '';
foreach( array( 'author', 'category', 'comments', 'guid' ) as $n )
if( $v = htmlspecialchars($item[$n]) )
$custom .= "\t\t<$n>$v</$n>\n";
$item['custom'] = $custom;
array_push( $rss_items, $item );
}
$rss->LadujDane( 'item', $rss_items );
$rss->LadujSzablon( 'items', <<<EOF
<item_block> <item>
<title>&item:title;</title>
<link>&item:link;</link>
<description>&item:description;</description>
<pubDate>&item:pubdate;</pubDate>
&item:custom;
</item>
</item_block>
EOF
);
die( $rss->Parsuj('rss') );
}
?>