Spracovanie RSS a Atom kanála
Zadanie
Skript načíta RSS alebo Atom XML a pomocou objektu SimpleXML ho prevedie do HTML kódu, ktorý odovzdá na výstupe.
Riešenie
Použijeme Typo3 objekt USER (include PHP script).
Typoscript
includeLibs.rss = fileadmin/sitename/inc/parser_rss.inc
temp.parserRss = USER
temp.parserRss {
userFunc = user_parser->parser
wrap = <div class="parserBox"><h4 class="headerColor headerYellow">Tehotenstvo</h4><div class="parserBoxIn hColorYellow">|</div></div>
# Setup for RSS
# url = sitename.sk/fileadmin/sitename/temp/rss.xml
# type =
# Setup for Atom
url = sitename.sk/fileadmin/sitename/temp/atom.xml
type = atom
}
PHP (parser_rss.inc)
<?php
/**
* Parse RSS
*
* @author Robert Gonda
*/
class user_parser {
/**
* Parse RSS file
*
* @param string $content:
* @param array $conf: TS configuration
* @return string return: $content
*/
function parser( $content, $conf ) {
$xml = simplexml_load_file( $conf['url'] );
if( !empty( $xml ) ) {
// Atom XML
if( $conf['type'] == 'atom' ) {
$i = 1;
foreach( $xml->entry as $key => $entry ) {
ereg( '([0-9]{4})\-([0-9]{1,2})\-([0-9]{1,2})', $entry->updated, $regs );
$updated = sprintf( '%02d.%02d.%04d', $regs[3], $regs[2], $regs[1] );
$out .= '
<div class="parserBoxItem">
<p class="author"><span class="date">'.$updated.'</span>'.( $i == 1 ? ' '.$entry->author->name : '' ).'</p>
<h4><a href="'.$entry->link['href'].'" title="'.$entry->title.'">'.$entry->title.'</a></h4>
'.( $i == 1 ? '<p>'.$entry->summary.'</p>' : '' ).'
</div>
';
$i++;
if( $i > 3 ) break;
}
}
// RSS XML
else {
foreach( $xml->channel as $channel ) {
$i = 1;
foreach( $channel->item as $key => $item ) {
$out .= '
<div class="parserBoxItem">
<h4><a href="'.$item->link.'" title="'.$item->title.'">'.$item->title.'</a></h4>
'.( $i == 1 ? '<p>'.$item->description.'</p>' : '' ).'
</div>
';
$i++;
if( $i > 3 ) break;
}
}
}
} else {
$out = 'Nedostupny zdroj RSS.';
}
return str_replace( '|', $out, $conf['wrap'] );
}
}
?>
| Žiadne dokumenty ani odkazy k tejto stránke. | ||
Generátor typoscriptu
Vytvára typoscript pre bežné použitie. Generátor je jednoduchý na obsluhu, stačí vyplniť zopár položiek formulára.