Feed Reader in Php

« Older   Newer »
  Share  
view post Posted on 3/9/2009, 03:23
Avatar

Staff

Group:
ForumFree Staff
Posts:
7,114

Status:


Feed Reader in Php


In questa guida verrà spiegato come realizzare un Feed Reader in php: l'unico requisito è uno spazio web che supporti php5.
Assicuriamoci che la cartella dove posizioneremo il file php abbia i permessi di scrittura, il nostro file dovrà infatti scrivere dei documenti di testo contenenti informazioni sull'ultima apertura del feed e la cache di quest'ultimo.
Al fine di evitare la ripetuta apertura del feed, lo script creerà una cache con un file di testo, limitando le aperture a 30 minuti di distanza ciascuna.
Vediamo ora lo script:
SPOILER (click to view)
CODICE
<?php

/*
*********************************************
* Feed Reader v1.0
* Distribuito dal NewsBoard Forum
* http://newsboard.forumfree.net/
* Autore: ~ Jolteon
* Data: 30/08/09, 12:31
*********************************************
*/

class Reader {
       
       var $feed         = '';
       var $source        = '';
       var $input         = array();
       
       function Reader( $mode=0, $feed='' )
       {
               global $_GET, $_POST;
               
               $this->input        = array_merge( $_GET, $_POST );
               
               if ( $mode == 0 )
               {
                       if ( !$this->input['feed'] )
                       {
                               echo "Non è stato specificato nessun feed!";
                               return;
                       }
                       $this->feed         = $this->input['feed'];
               }
               else
               {
                       $this->feed        = $feed;
               }
               if ( !file_exists( 'time.txt' ) || @file_get_contents( 'time.txt' ) + 1800 < time() )
               {
                       $this->source        = implode( '', file( $this->feed ) );
                       $this->make_txt( 'time', time() );
                       $this->make_txt( 'cache', $this->source );
               }
               else
               {
                       $this->source        = file_get_contents( 'cache.txt' );
               }        
               $this->source = str_replace( "\n", "", $this->source );
               
               $this->print_page();
       }
       
       function make_txt( $file, $text )
       {
               $fp = fopen( $file . '.txt', 'w' );
               fwrite( $fp, $text );
               fclose( $fp );
       }
       
       function print_page()
       {
               $data        = $this->elabora_dati();
               foreach ( $data['feeds'] as $v )
               {
                       $output .= $this->entry( $v );
               }
               echo $this->page( $data['title'], $output );
       }
       
       function elabora_dati()
       {
               preg_match( '/<title>(.*)<\/title>/U', $this->source, $title );
               preg_match_all( '/<item>(.*)<\/item>/U', $this->source, $items );
               $feed = 0;
               foreach ( $items[1] as $v )
               {
                       ++$feed;
                       preg_match( '/<title>(.+?)<\/title>/U', $v, $tit );
                       preg_match( '/<link>(.*)<\/link>/U', $v, $link );
                       preg_match( '/<description>(.*)<\/description>/U', $v, $desc );
                       preg_match( '/<pubDate>(.*)<\/pubDate>/U', $v, $date );
                       $res[ $feed ] = array(
                                                         'title'        => $tit[1],
                                                         'link'        => $link[1],
                                                         'desc'        => $this->parse_feed( $desc[1] ),
                                                         'date'        => $this->render_date( $date[1] ),
                                                       );
               }
               return array(
                                        'title' => $title[1],
                                        'feeds' => $res,
                                        );
       }
       
       function parse_feed( $desc )
       {
               $search  = array( '&amp;', '&lt;', '&gt;', '&quot;' );
               $replace = array( '&', '<', '>', '"' );
               return str_replace( $search, $replace, $desc );
       }
       
       function render_date( $date )
       {
               $data = explode( ' ', $date );
               $mesi = array(
                                         'Jan' => 1,
                                         'Feb'        => 2,
                                         'Mar' => 3,
                                         'Apr' => 4,
                                         'May' => 5,
                                         'Jun' => 6,
                                         'Jul' => 7,
                                         'Aug' => 8,
                                         'Sep' => 9,
                                         'Oct' => 10,
                                         'Nov' => 11,
                                         'Dec' => 12
                                         );
               $oth  = explode( ':', $data[4] );
               switch( $data[5] )
               {
                       case 'CET':
                               $oth[0] += 1;
                               break;
                       case 'CEST':
                               $oth[0] += 2;
                               break;
               }
               $time = mktime( intval( $oth[0] ), $oth[1], $oth[2], $mesi[ $data[2] ], $data[1], $data[3] );
               if ( $time > 0 )
               {
                       return @date( 'd/m/y, H:i', $time );
               }
               else
               {
                       return 'data sconosciuta';
               }
       }
       
       function page( $titolo, $contenuto )
       {
               return "<html>
                       <head>
                               <title>{$titolo}</title>
                               </head>
                               <body>
                               <h1>{$titolo}</h1>
                               {$contenuto}
                               </body>
                               </html>";
       }
       
       function entry( $f )
       {
               return "<div style='border:1px dashed #000; margin: 5px; padding: 4px'>
                       <a href='{$f['link']}'>{$f['title']}</a><br>
                               Pubblicato il {$f['date']}<br>
                               {$f['desc']}
                       </div>";
       }
       
}

?>

Le uniche cose che dovete modificare sono le funzioni Reader::page() e Reader::entry(): queste ultime contengono il codice html di output per l'impaginazione dei feeds.
All'interno della funzione Reader::page() avrete solo due parametri, $titolo che contiene il titolo del feed, $contenuto che contiene i vari feeds già impaginati.
La funzione Reader::entry(), invece, serve per l'impaginazione di un sigolo item: al suo interno potete usare queste variabili:
$f['title'] -> titolo del feed
$f['link'] -> link del feed
$f['desc'] -> descrizione del feed
$f['date'] -> data di pubblicazione

Tutte queste variabili sono già usate nella visualizzazione di default, potete togliere e inserire quelle che volete.
Per quanto riguarda il suo utilizzo, potete avviarlo in due modi diversi: inserendo un link ad un feed fisso oppure tramite metodo Get o Post; in cima al file php (o anche in fondo, non influisce sul funzionamento) assegnate la class Reader ad una variabile eseguendo l'omonima funzione, in questo modo:
CODICE
$feed = new Reader();

In questo modo dovrete inserire il link al feed nell'url, aggiungendo ?feed=link, ottenendo un indirizzo simile: http://tuosito.it/Reader.php?feed=http://n...ss.php?c=121718
Se invece volete mantenere fisso il feed, avviate la funzione in questo modo, inserendo direttamente tra i suoi parametri il link al feed:
CODICE
$feed = new Reader( 1, 'http://newsboard.forumfree.net/rss.php?c=121718' );
 
Web  Top
0 replies since 3/9/2009, 03:23   931 views
  Share