Registreren

    Informatie

    Het is heel gemakkelijk om lid te worden. Door je gegevens op te geven op de registratie pagina ontvang je automatisch een email met je inlog gegevens.

  • Registreer je hier

Aanmelden

    Informatie

    Omdat je niet aangemeld bent is het gebruikerspaneel niet beschikbaar. Vul hiernaast je gegevens in om gebruik te maken van het gebruikerspaneel.

Aanmeldformulier


RSS-feed reader

PHP is een scripttaal, die bedoeld is om op webservers dynamische webpagina's te creëren. PHP is in 1994 ontworpen door Rasmus Lerdorf, een senior software engineer bij IBM. Destijds was de taal duidelijk geïnspireerd door Perl.

Aanvankelijk stonden de letters PHP voor Personal Home Page (de volledige naam was Personal Home Page/Forms Interpreter, PHP/FI). Sinds PHP 3.0 is de betekenis een recursief acroniem geworden: PHP: Hypertext Preprocessor.

RSS-feed reader

Berichtdoor RedRose » 03 Sep 2008 21:12

Inleiding
Ik heb een RSS-parser geschreven die gemakkelijk gebruikt kan worden om feeds van bijvoorbeeld nu.nl in te lezen en weer te geven in de browser.

Ik heb twee klassen geschreven: FeedReader en FeedEntry. FeedReader wordt gebruikt om de gehele feed te parsen, terwijl FeedEntry één enkel RSS-item bevat.

De parser geeft de titel, URL en beschrijving weer van de feed zelf en van elk van zijn items.

Er zijn drie mogelijke encodingmanieren: ISO-8859-1, UTF-8 en US-ASCII.

De mogelijkheid bestaat een tabdiepte op te geven, waardoor de uitgepoepte HTML er netjes uit ziet.

Er worden verschillende variabelen gedefiniëerd, onder andere TAB (tab) en NLN (newline).


Eisen

  • PHP5
  • SAX-parser

Gebruik

PHP
Code: Alles selecteren
<?php echo '<?xml version="1.0" encoding="ISO-8859-1"?>'."\n"; ?>
<html>
   <head>
      <title>FeedReader - test</title>
   </head>
   <body>
<?php
      require_once
( 'rss.php' );
      $rss = new FeedReader();
      
      
// Set the URL for the feedreader.
      $rss->setURL( 'http://www.nu.nl/deeplink_rss2/?r=Wetenschap' );
      
      
// Set the encoding for the feedreader.
      $rss->setEncoding( 'ISO-8859-1' );
      
      
// Set the tabdepth for the feedreader.
      $rss->setDepth( 2 );
      
      
// Parse the feed.
      echo $rss->parse().NLN;
?>
   </body>
</html>


Code

PHP
Code: Alles selecteren
<?php
/**
* Copyright (c) 2005, Jorn van der Pol
* All rights reserved.
*
* Redistribution of source is permitted, provided that the above
* copyrightmessage, this condition and following disclaimer are retained.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHTHOLDER AND CONTRIBUTORS "AS-IS".
* BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE THERE IS NO WARRANTY OF
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF FITNESS AND MERCHANTABILITY FOR A PARTICULAR PURPOSE.
*
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU.
* SHOULD THE SOFTWARE PROOF DEFECT, YOU ASSUME THE COST OF ALL NECESSARY
* SERVICING, REPAIR OR CORRECTION.
*
* IN NO EVENT, UNLESS STATED OTHERWISE OR REQUIRED BY APPLICABLE LAW, WILL THE
* COPYRIGHTHOLDER OR ANY CONTRIBUTOR BE LIABLE TO YOU FOR ANY GENERAL, SPECIAL
* INCIDENTAL OR CONSEQUENTIAL DAMAGE CAUSED BY THE SOFTWARE, INCLUDING, BUT NOT
* LIMITED TO, LOSS OF DATA AND COMPUTERMALFUNCTION, EVEN IF THE COPYRIGHTHOLDER
* OR CONTRIBUTOR IS AWARE OF THE POSSIBILITY OF SUCH RISKS.
*/

/**
* Author      : Jorn van der Pol
* Email      : ikbenconsequent at hotmail dot com
*/

// Define TAB.
define( 'TAB', "\t" );
// Define newline.
define( 'NLN', "\n" );

// Define entities to specify certain types of tags within the parser.
define( 'LINK', 1 );
define( 'TITLE', 2 );
define( 'DESC', 3 );

/**
* This class represents an entry inside the RSS-feed.
*/
class FeedEntry
{
   private $title = '';
   private $URL = '';
   private $description = '';
   private $depth = 0;

   /**
    * Sets the title for this item.
    * @param $title The title to set.
    */
   public function setTitle( $title )
   {
      $this->title .= str_replace(array("\r", "\n"), '', $title);
   }
   
   /**
    * Sets the URL for this item.
    * @param $url The URL to set.
    */
   public function setURL( $URL )
   {
      $this->URL .= $URL;
   }
   
   /**
    * Sets the description for this item.
    * @param $description The description to set.
    */
   public function setDescription( $description )
   {
      if( empty( $this->description ) )
         $this->description = str_replace( array( "\r", "\n" ), '', $description );
   }
   
   /**
    * Sets the tabdepth for this item.
    * @param $depth The tabdepth to set.
    */
   public function setDepth( $depth )
   {
      if( $depth < 0 )
         $depth = 0;
      $this->depth = $depth;
   }
   
   /**
    * Parses this item.
    * @return string
    */
   public function parse()
   {
      return str_repeat( TAB, $this->depth + 1 ).'<li><h2><a href="'.$this->URL.'">'.$this->title.'</a></h2>'.NLN.
         str_repeat( TAB, $this->depth + 2 ).'<span>'.$this->description.'</span></li>';
   }
}

/**
* The class to parse RSS-feeds.
*/
class FeedReader
{
   private $URL = '';
   private $information = array();
   private $entries = array();
   private $text = '';
   private $initem = false;
   private $status = 0;
   private $depth = 0;
   private $encoding = 'ISO-8859-1';
   
   /*
    * Sets the URL which locates the RSS-feed.
    * @param $url The URL which locates the RSS-feed.
    */
   public function setURL( $URL )
   {
      $this->URL = $URL;
   }
   
   /**
    * Returns the URL which locates the RSS-feed.
    * @return string
    */
   public function getURL()
   {
      return $this->URL;
   }
   
   /**
    * Sets the tabdepth for the output.
    * @param $depth The tabdepth to set.

    */
   public function setDepth( $depth )
   {
      if( $depth < 0 )
         $depth = 0;
      $this->depth = $depth;
   }
   
   /**
    * Returns the tabdepth.
    * @return int
    */
   public function getDepth()
   {
      return $this->depth;
   }
   
   /**
    * Sets the encoding for the SAX-parser. Possible values are ISO-8859-1, UTF-8 and US-ASCII.
    * @param $encoding The encoding to set.
    */
   public function setEncoding( $encoding )
   {
      // This array contains all possible values for $encoding.
      $codes = array( 'ISO-8859-1', 'UTF-8', 'US-ASCII' );
      if( in_array( $encoding, $codes ) )
         $this->encoding = $encoding;
      else
         die( 'FeedReader: Invalid encoding: <em>'.$encoding.'</em>' );
   }
   
   /**
    * Returns the current encoding.
    * @return string.
    */
   public function getEncoding()
   {
      return $this->encoding;
   }
   
   /**
    * Parses a starting element inside the XML-document.
    * @param $parser The parser used to parse the document.
    * @param $name The name of the element.
    * @param $attrs The attributes assigned to this element.
    */
   private function startElement( $parser, $name, $attrs )
   {
      if( $name == 'ITEM' )
      {
         $this->initem = true;
         $this->entries[] = new FeedEntry();
      }
      elseif( $name == 'DESCRIPTION' )
      {
         $this->status = DESC;
      }
      elseif( $name == 'TITLE' )
      {
         $this->status = TITLE;
      }
      elseif( $name == 'LINK' )
      {
         $this->status = LINK;
      }
   }
   
   /**
    * Parses a ending element inside the XML-document.
    * @param $parser The parser used to parse the document.
    * @param $name The name of the element.
    */
   private function endElement( $parser, $name )
   {
      $this->status = 0;
     
      if( $name == 'ITEM' )
         $this->initem = false;
   }
   
   /**
    * Parses data inside the XML-document.
    * @param $parser The parser used to parse the document.
    * @param $value The value of the data.
    */
   private function characterData( $parser, $value )
   {
      if( $this->initem )
      {
         // If we are inside an item, update the current item.
         switch( $this->status )
         {
            case LINK:
               $this->entries[sizeof( $this->entries) - 1 ]->setURL( $value );
               break;
            case TITLE:
               $this->entries[sizeof( $this->entries) - 1 ]->setTitle( $value );
               break;
            case DESC:
               $this->entries[sizeof( $this->entries) - 1 ]->setDescription( $value );
               break;
            default: break;
         }
      }
      else
      {
         // If we are not inside an item, update the information for this feed.
         switch( $this->status )
         {
            case LINK:
               $this->information['link'] = $value;
               break;
            case TITLE:
               $this->information['title'] = $value;
               break;
            case DESC:
               $this->information['description'] = $value;
               break;
            default: break;
         }
      }
   }
   
   /**
    * Read the document specified by private field URL.
    * @return string
    */
   private function read()
   {
         if( $this->getURL() == '' )
            die( 'FeedReader: No URL set.' );
         else
            return file_get_contents( $this->getURL() );
   }
   
   /**
    * Parses the feed.
    * @return string
    */
   public function parse()
   {
      $this->text = $this->read();
     
      if( empty( $this->text ) )
         die( 'FeedReader: No data found at URL'.$this->getURL() );
     
      // Create an XML-parser to parse the RSS-document.
      $prs = xml_parser_create( $this->encoding );
      xml_parser_set_option( $prs, XML_OPTION_CASE_FOLDING, 1 );
      xml_set_object( $prs, $this );
      xml_set_element_handler( $prs, 'startElement', 'endElement' );
      xml_set_character_data_handler( $prs, 'characterData' );
     
      // Parse the XML-document.
      xml_parse( $prs, $this->text, true );
      xml_parser_free( $prs );
     
      $this->text = '';
     
      // Parse the information of this feed.
      $this->text .= str_repeat( TAB, $this->depth ).'<h1><a href="'.$this->information['link'].'">'.$this->information['title'].'</a></h1>'.NLN.
         str_repeat( TAB, $this->depth ).'<h6>'.$this->information['description'].'</h6>'.NLN.
         NLN.
         str_repeat( TAB, $this->depth ).'<ul>'.NLN;
     
      // Parse all entries.
      foreach( $this->entries as $entry )
      {
         $entry->setDepth( $this->depth );
         $this->text .= $entry->parse().NLN;
      }
     
      return $this->text.str_repeat( TAB, $this->depth ).'</ul>';
   }
}
?>
RedRose
Globale moderator
 
Berichten: 1994
Geregistreerd: 14 Jun 2005 18:12

Terug naar PHP



Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 1 gast


cron