RSS Feed Script Parser Help

Discussion in 'PHP' started by Fl1p, Mar 24, 2007.

  1. #1
    Hello,

    Im currently using this script http://www.sitepoint.com/examples/phpxml/sitepointcover.php.txt to display RSS feed on my website.

    The script works properly as seen here as an example:
    http://www.grabimage.com/feed.php

    What I want to do though is limit the amount of news items being displayed on the page. On the feed above there is 9 news items, lets say I want to limit that to 5 items... Can I edit the script above so it will only display 5 items instead of all the items on a particular feed? If so can anyone please show me how to do this...

    Sorry if I do not make sense... Any help will be greatly appreciated.

    Thanks
     
    Fl1p, Mar 24, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    php5 I hope :

    
    <?
    class rss
    {
    		public static   $url,
    						$limit,
    						$encoding,
    						$struct,
    						$parser,
    						$raw,
    						$links,
    						$descs ;
    	
    	function __construct( $url, 
    						  $limit = 5, 
    						  $encoding = null, 
    						  $links = "<dt><b><a href='%s'>%s</a></b></dt>", 
    						  $descs = "<dd>%s</dd>" )
    	{
    		self::$limit = $limit ;
    		self::$encoding = trim( $encoding ) ;
    		self::$url = trim( $url ) ;
    		self::$links = $links;
    		self::$descs = $descs;
    			
    		if( !( self::$parser = xml_parser_create( self::$encoding ) ) ) :
    			print("xml::error : Cannot create parser");
    		elseif( !xml_set_object( self::$parser, $this ) ) :
    			print("xml:error : Cannot set object for parser");
    		elseif( !xml_set_element_handler( self::$parser, array( "self", "xmlStart"), array( "self", "xmlEnd")) ):
    			print("xml::error : Cannot set element handler");
    		elseif( !xml_set_character_data_handler( self::$parser, array( "self", "xmlChard") ) ):
    			print("xml::error : Cannot set data handler");
    		elseif( !(self::$raw = file_get_contents( self::$url ) ) ) :
    			print("xml::error : Cannot retrieve rss from " . self::$url );
    		elseif( !xml_parse( self::$parser, self::$raw )) :
    			self::getError( );
    		endif;
    		
    		@xml_parser_free( self::$parser );
    	}
    	function xmlStart( $parser, $name, $attrs )
    	{
    		if ( self::$struct['insideitem'] ): self::$struct['tag'] = $name;
    		elseif( $name == "ITEM" ): self::$struct['insideitem'] = true;
    		endif;
    	}
    	function xmlEnd( $parser, $name )
    	{
    		if( !self::$limit ) return;
    		
    		if( $name == "ITEM" ):
    			printf( self::$links, trim( self::$struct['link'] ), htmlspecialchars(trim( self::$struct['title'] )));
    			printf( self::$descs, htmlspecialchars( trim( self::$struct['description'] ) ));
    			self::$limit--;
    		endif;
    	}
    	function xmlChard( $parser, $data )
    	{
    		if( self::$struct['insideitem'] ) :
    			switch( self::$struct['tag'] ) :
    				case "TITLE" : self::$struct['title'] .= $data ; break;
    				case "DESCRIPTION" : self::$struct['description'] .= $data; break;
    				case "LINK" : self::$struct['link'] .= $data ; break;
    			endswitch;
    		endif;	
    	}
    	function getError( )
    	{
    		printf("xml::error : %s at line %d", xml_error_string(xml_get_error_code( self::$parser ) ), 
    											 xml_get_current_line_number( self::$parser )
    			  );
    	}
    }
    ?>
    
    PHP:
    save that file as something.php, then where you want the content displayed on the page use the following code to get it there.
    
    <? include("something.php"); new rss("http://www.sitepoint.com/rss.php"); ?>
    
    PHP:
    prototype :
    void new rss( string url, [ int limit, [ string encoding, [ string links, [ string descs ] ] ] ] );

    defaults :
    limit = 5
    encoding = null
    links = "<dt><b><a href='%s'>%s</a></b></dt>"
    descs = "<dd>%s</dd>"

    that was fun thanks .....
     
    krakjoe, Mar 25, 2007 IP
    Fl1p likes this.
  3. Fl1p

    Fl1p Active Member

    Messages:
    511
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thank you very much for your help.

    However im not on PHP5, my PHP version is 4.4.2 and when I tried to run the script I got the error below...

    
    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/x/public_html/include/rssfeed.php on line 4
    
    Code (markup):
    Can you edit the script so it will work on my PHP version or maybe I just did something wrong?

    Green rep added!
     
    Fl1p, Mar 25, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    <?
    class rss
    {
    		var   			$url,
    						$limit,
    						$encoding,
    						$struct,
    						$parser,
    						$raw,
    						$links,
    						$descs ;
    	
    	function rss( $url, 
    						  $limit = 5, 
    						  $encoding = null, 
    						  $links = "<dt><b><a href='%s'>%s</a></b></dt>", 
    						  $descs = "<dd>%s</dd>" )
    	{
    		$this->limit = $limit ;
    		$this->encoding = trim( $encoding ) ;
    		$this->url = trim( $url ) ;
    		$this->links = $links;
    		$this->descs = $descs;
    			
    		if( !( $this->parser = xml_parser_create( $this->encoding ) ) ) :
    			print("xml::error : Cannot create parser");
    		elseif( !xml_set_object( $this->parser, $this ) ) :
    			print("xml:error : Cannot set object for parser");
    		elseif( !xml_set_element_handler( $this->parser, array( $this, "xmlStart"), array( $this, "xmlEnd")) ):
    			print("xml::error : Cannot set element handler");
    		elseif( !xml_set_character_data_handler( $this->parser, array( "self", "xmlChard") ) ):
    			print("xml::error : Cannot set data handler");
    		elseif( !($this->raw = file_get_contents( $this->url ) ) ) :
    			print("xml::error : Cannot retrieve rss from " . $this->url );
    		elseif( !xml_parse( $this->parser, $this->raw )) :
    			$this->getError( );
    		endif;
    		
    		xml_parser_free( $this->parser );
    	}
    	function xmlStart( $parser, $name, $attrs )
    	{
    		if ( $this->struct['insideitem'] ): $this->struct['tag'] = $name;
    		elseif( $name == "ITEM" ): $this->struct['insideitem'] = true;
    		endif;
    	}
    	function xmlEnd( $parser, $name )
    	{
    		if( !$this->limit ) return;
    		
    		if( $name == "ITEM" ):
    			printf( $this->links, trim( $this->struct['link'] ), htmlspecialchars(trim( $this->struct['title'] )));
    			printf( $this->descs, htmlspecialchars( trim( $this->struct['description'] ) ));
    			$this->limit--;
    		endif;
    	}
    	function xmlChard( $parser, $data )
    	{
    		if( $this->struct['insideitem'] ) :
    			switch( $this->struct['tag'] ) :
    				case "TITLE" : $this->struct['title'] .= $data ; break;
    				case "DESCRIPTION" : $this->struct['description'] .= $data; break;
    				case "LINK" : $this->struct['link'] .= $data ; break;
    			endswitch;
    		endif;	
    	}
    	function getError( )
    	{
    		printf("xml::error : %s at line %d", xml_error_string(xml_get_error_code( $this->parser ) ), 
    											 xml_get_current_line_number( $this->parser )
    			  );
    	}
    }
    //void new rss( string url, [ int limit, [ string encoding, [ string links, [ string descs ] ] ] ] );
    new rss( "http://www.sitepoint.com/rss.php" );
    
    PHP:
    Try that first, dunno if php is going to handle the object properly.
     
    krakjoe, Mar 25, 2007 IP
  5. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hamidof, Mar 25, 2007 IP
  6. Fl1p

    Fl1p Active Member

    Messages:
    511
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #6
    @krakjoe

    Thanks for the follow up..

    I saved the code above (rssfeed.php) and then use PHP include to display the feed <php include("include/rssfeed.php"); ?> and now im getting the error below..

    
    Warning: xml_parse(): Unable to call handler in /home/x/public_html/include/rssfeed.php on line 35
    
    Code (markup):
    Any idea?

    @hamidof
    That script looks a bit complex to me, the one I currently have gets the job done.. it's just I wanted to limit the amount feed being displayed.. I will try it out..
     
    Fl1p, Mar 25, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    
    <?
    class rss
    {
    		var   			$url,
    						$limit,
    						$encoding,
    						$struct,
    						$parser,
    						$raw,
    						$links,
    						$descs ;
    	
    	function __construct( $url, 
    						  $limit = 5, 
    						  $encoding = null, 
    						  $links = "<dt><b><a href='%s'>%s</a></b></dt>", 
    						  $descs = "<dd>%s</dd>" )
    	{
    		$this->limit = $limit ;
    		$this->encoding = trim( $encoding ) ;
    		$this->url = trim( $url ) ;
    		$this->links = $links;
    		$this->descs = $descs;
    			
    		if( !( $this->parser = xml_parser_create( $this->encoding ) ) ) :
    			print("xml::error : Cannot create parser");
    		elseif( !xml_set_object( $this->parser, $this ) ) :
    			print("xml:error : Cannot set object for parser");
    		elseif( !xml_set_element_handler( $this->parser, "xmlStart",  "xmlEnd" ) ):
    			print("xml::error : Cannot set element handler");
    		elseif( !xml_set_character_data_handler( $this->parser, "xmlChard" ) ):
    			print("xml::error : Cannot set data handler");
    		elseif( !($this->raw = file_get_contents( $this->url ) ) ) :
    			print("xml::error : Cannot retrieve rss from " . $this->url );
    		elseif( !xml_parse( $this->parser, $this->raw )) :
    			$this->getError( );
    		endif;
    		
    		xml_parser_free( $this->parser );
    	}
    	function xmlStart( $parser, $name, $attrs )
    	{
    		if ( $this->struct['insideitem'] ): $this->struct['tag'] = $name;
    		elseif( $name == "ITEM" ): $this->struct['insideitem'] = true;
    		endif;
    	}
    	function xmlEnd( $parser, $name )
    	{
    		if( !$this->limit ) return;
    		
    		if( $name == "ITEM" ):
    			printf( $this->links, trim( $this->struct['link'] ), htmlspecialchars(trim( $this->struct['title'] )));
    			printf( $this->descs, htmlspecialchars( trim( $this->struct['description'] ) ));
    			$this->limit--;
    		endif;
    	}
    	function xmlChard( $parser, $data )
    	{
    		if( $this->struct['insideitem'] ) :
    			switch( $this->struct['tag'] ) :
    				case "TITLE" : $this->struct['title'] .= $data ; break;
    				case "DESCRIPTION" : $this->struct['description'] .= $data; break;
    				case "LINK" : $this->struct['link'] .= $data ; break;
    			endswitch;
    		endif;	
    	}
    	function getError( )
    	{
    		printf("xml::error : %s at line %d", xml_error_string(xml_get_error_code( $this->parser ) ), 
    											 xml_get_current_line_number( $this->parser )
    			  );
    	}
    }
    //void new rss( string url, [ int limit, [ string encoding, [ string links, [ string descs ] ] ] ] );
    new rss( "http://www.sitepoint.com/rss.php" );
    
    PHP:
    try that....
     
    krakjoe, Mar 26, 2007 IP
  8. Lukaslo

    Lukaslo Peon

    Messages:
    751
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Lukaslo, Mar 26, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    my apologies I could / should have done this first instead of messing about :

    
    <html>
    <head>
    <title> Current SitePoint Articles </title>
    </head>
    <body>
    <h2>Currently on SitePoint.com...</h2>
    <dl>
    <?php
    
    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $limit = 5;
    
    function startElement($parser, $name, $attrs) {
    	global $insideitem, $tag, $title, $description, $link;
    	if ($insideitem) {
    		$tag = $name;
    	} elseif ($name == "ITEM") {
    		$insideitem = true;
    	}
    }
    
    function endElement($parser, $name) {
    	global $insideitem, $tag, $title, $description, $link, $limit ;
    	if( !$limit ) return;
    	if ($name == "ITEM") {
    		printf("<dt><b><a href='%s'>%s</a></b></dt>",
    			trim($link),htmlspecialchars(trim($title)));
    		printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
    		$title = "";
    		$description = "";
    		$link = "";
    		$insideitem = false;
    		$limit--;
    	}
    }
    
    function characterData($parser, $data) {
    	global $insideitem, $tag, $title, $description, $link;
    	if ($insideitem) {
    	switch ($tag) {
    		case "TITLE":
    		$title .= $data;
    		break;
    		case "DESCRIPTION":
    		$description .= $data;
    		break;
    		case "LINK":
    		$link .= $data;
    		break;
    	}
    	}
    }
    
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen("http://www.sitepoint.com/rss.php","r")
    	or die("Error reading RSS data.");
    while ($data = fread($fp, 4096))
    	xml_parse($xml_parser, $data, feof($fp))
    		or die(sprintf("XML error: %s at line %d", 
    			xml_error_string(xml_get_error_code($xml_parser)), 
    			xml_get_current_line_number($xml_parser)));
    fclose($fp);
    xml_parser_free($xml_parser);
    
    ?>
    </dl>
    </body>
    </html>
    
    PHP:
     
    krakjoe, Mar 26, 2007 IP
  10. Fl1p

    Fl1p Active Member

    Messages:
    511
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #10
    krakjoe the last code you provided works perfectly!

    Thanks you very much for your help! :)
     
    Fl1p, Mar 26, 2007 IP