Magazine Informatica

Generare i Feed RSS con PHP

Creato il 07 aprile 2012 da Ketek @CarloVentrella
Feed RSS

Ecco uno script in PHP che vi permette di generare i Feed RSS in modo semplice e veloce.

Si tratta di una classe, RSS, che contiene 4 funzioni:

•   creaRSS

•   aggiungi_immagine

•   aggiungi_articolo

•   output

Ma passiamo subito alla visualizzazione del codice:

<?php

class RSS

{

// variabili ------>

var $title = ''; // titolo del canale

var $link = ''; // link del canale

var $description = ''; // descrizione del canale

var $data = ''; // data dell'articolo

var $cont = '';

// ----------------->

function creaRSS($title, $link, $description)

{

// inizializzo variabili

$this->title = $title;

$this->link = $link;

$this->description = $description;

}

function aggiungi_immagine($title, $url, $link, $description = '')

{

//riempio l'array image

$this->image['title'] = $title;

$this->image['url'] = $url;

$this->image['link'] = $link;

$this->image['description'] = $description;

// ottengo le dimensioni dell'immagine

if( $tmp = @getimagesize($url) ){

$this->image['w'] = ($tmp[0] > 144) ? 144 : $tmp[0];

$this->image['h'] = ($tmp[1] > 400) ? 400 : $tmp[1];

}

}

function aggiungi_articolo($title, $link, $description, $author, $categoria,$data)

{

// inserisce un nuovo articolo

global $cont;

$cont .= "<item>" .

"<title>".$title."</title>" .

"<description>".$description."</description>" .

"<link>".$link."</link>" .

"<author>".$author."</author>" .

"<category>".$categoria."</category>" .

"<data>".$data."</data>" .

"</item>";

}

function output($save = false, $path = '')

{

global $cont;

// header

$out = '<?xml version="1.0" encoding="utf-8"?>' . "" .

// l'il foglio di stile è opzionale

'<?xml-stylesheet type="text/css" href="rss.css" ?>' . "" .

'<rss version="2.0">' . "" .

'<channel>' . "";

// descrivo il canale

$out .= "<title>".$this->title."</title>" .

"<link>".$this->link."</link>" .

"<description>".$this->description."</description>";

// Aggiungo l'immagine se è stata inserita

if($this->image['title'] & $this->image['url'] & $this->image['link'])

{

$out .= "<image>" .

"<title>" . $this->image['title'] . "</title>" .

"<url>" . $this->image['url'] . "</url>" .

"<link>" . $this->image['link'] . "</link>";

if( $this->image['description'] ) {

$out .= "<description>".$this->image['description']."</description>";

}

if( $this->image['w'] and $this->image['h'] ) {

$out .= "<width>" . $this->image['w'] . "</width>" .

"<height>" . $this->image['h'] . "</height>";

}

$out .= "</image>";

}

// aggiungo tutti gli articoli

$out .= $cont;

// chiudo il canale

$out .= "</channel></rss>";

// True output

if( !$save or !$path ){

header("Content-type: application/xml");

echo $out;

return true;

}

else

{

$fh = fopen($path, 'w');

if($fh) {

fwrite($fh, $out);

fclose($fh);

return true;

}

return false;

}

}

}

?>

Copia tutto

E questo è un esempio di output:

$rss = new RSS;

$rss->creaRSS('Classici del 900', 'http://classici900.it', 'Tutto sui classici del 900');

$rss->aggiungi_immagine();

$rss->aggiungi_articolo('Il visconte dimezzato',

   'classici900.it/il_visconte_dimezzato.html',

   'Il visconte dimezzato è il primo capitolo della..',

   'I. Calvino',

   'Classici - Calvino',

   'Sat, 07 Apr 2012 20:47:39 +1000'

);

$rss->aggiungi_articolo('Il cavaliere inesistente',

   'classici900.it/il_cavaliere_inesistente.html',

   'Il cavaliere inesistente è il terzo capitolo della...',

   'I. Calvino',

   'Classici - Calvino',

   'Sat, 07 Apr 2012 20:51:48 +1000');

$r->Output(true, './rss.xml');

Copia tutto

L'output potrete visualizzarlo nel file rss.xml che, se non esite, sarà appositamente creato!



Potrebbero interessarti anche :

Ritornare alla prima pagina di Logo Paperblog

Possono interessarti anche questi articoli :

Dossier Paperblog