Need help with my RSS feed script.

Discussion in 'PHP' started by fadetoblack22, May 25, 2008.

  1. #1
    Hello, I basically want this to just display the top 4 stories and not the whole feed. I don't know what to change to make it to do that.

    Can someone offer any help?

    <?php
    /*
    
    Class RSSParser: 	2 October 2002
    
    Author:				Duncan Gough
    
    Overview:			An RSS parser that uses PHP and freely available RSS feeds to add fresh news content to your site.
    
    Installation:		Decompress the file into your webroot and include it from whichever pages on which you want
    					to display the data, e.g;
    
    					include("rss.php");
    
    Usage:				As above, just include the rss.php file from within your PHP page, and the news will appear.
    					You should find the HTML code in the functions endElement(), show_title() and show_list_box() below,
    					feel free to modify these to match your site.
    */
    
    class RSSParser	{
    
        var $title			= "";
        var $link 			= "";
        var $description 	= false;
        var $inside_item 	= false;
        
    	
    
    	function startElement( $parser, $name, $attrs='' ){
    		global $current_tag;
    
    		$current_tag = $name;
    
    		if( $current_tag == "ITEM" )
    			$this->inside_item = true;
    
    	} // endfunc startElement
    
    	function endElement( $parser, $tagName, $attrs='' ){
    		global $current_tag;
    
        	if ( $tagName == "ITEM" ) {
    
    			printf( "\t<br><b><a href='%s' target='_blank'>%s</a></b>\n", trim( $this->link ), htmlspecialchars( trim( $this->title ) ) );
        		printf( "\t<br>%s<br>\n", htmlspecialchars( trim( $this->description ) ) );
    
        		$this->title = "";
        		$this->description = "";
        		$this->link = "";
        		$this->inside_item = false;
    
        	}
    
    	} // endfunc endElement
    
    	function characterData( $parser, $data ){
    		global $current_tag;
    
    		if( $this->inside_item ){
    			switch($current_tag){
    
    				case "TITLE":
    					$this->title .= $data;
    					break;
    				case "DESCRIPTION":
    					$this->description .= $data;
    					break;
    				case "LINK":
    					$this->link .= $data;
    					break;
    
    				default:
    					break;
    
    			} // endswitch
    
    		} // end if
    
    	} // endfunc characterData
    
    	function parse_results( $xml_parser, $rss_parser, $file )	{
    
    		xml_set_object( $xml_parser, &$rss_parser );
    		xml_set_element_handler( $xml_parser, "startElement", "endElement" );
    		xml_set_character_data_handler( $xml_parser, "characterData" );
    
    		$fp = fopen("$file","r") or die( "Error reading XML file, $file" );
    
    		while ($data = fread($fp, 4096))	{
    		
    			// parse the data
    			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 ) ) );
    			
    		} // endwhile
    
    		fclose($fp);
    
    		xml_parser_free( $xml_parser );
    
    	} // endfunc parse_results
    
    
    } // endclass RSSParser
    
    global $rss_url;
    
    // Set a default feed
    if( $rss_url == "" )
    	$rss_url = "http://www.casinogamblingweb.com/gambling.xml";
    
    $xml_parser = xml_parser_create();
    $rss_parser = new RSSParser();
    
    $rss_parser->parse_results( $xml_parser, &$rss_parser, $rss_url );
    
    
    ?>
    PHP:

     
    fadetoblack22, May 25, 2008 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    /*
    
    Class RSSParser:    2 October 2002
    
    Author:    Duncan Gough
    
    Overview:         An RSS parser that uses PHP and freely available RSS feeds to add fresh news content to your site.
    
    Installation:      Decompress the file into your webroot and include it from whichever pages on which you want
                        to display the data, e.g;
    
                        include("rss.php");
    
    Usage:        As above, just include the rss.php file from within your PHP page, and the news will appear.
                        You should find the HTML code in the functions endElement(), show_title() and show_list_box() below,
                        feel free to modify these to match your site.
    */
    
    class RSSParser {
    
        var $title      	= "";
        var $link       	= "";
        var $description    = false;
        var $inside_item    = false;
    	var $limit 			= 0 ;
    	var $done			= 0 ;
    	
    	function RSSParser( $limit = 0 )
    	{
    		$this->limit = $limit ;
    	}
    
        function startElement( $parser, $name, $attrs='' ){
            global $current_tag;
    
            $current_tag = $name;
    
            if( $current_tag == "ITEM" )
                $this->inside_item = true;
    
        } // endfunc startElement
    
        function endElement( $parser, $tagName, $attrs='' ){
            global $current_tag;
    
            if ( $tagName == "ITEM" ) {
    
                printf( "\t<br><b><a href='%s' target='_blank'>%s</a></b>\n", trim( $this->link ), htmlspecialchars( trim( $this->title ) ) );
                printf( "\t<br>%s<br>\n", htmlspecialchars( trim( $this->description ) ) );
    
                $this->title = "";
                $this->description = "";
                $this->link = "";
                $this->inside_item = false;
    			
    			if( $this->limit and ( $this->limit > $this->done++ ) )
    			{
    				$this->stop = true ;
    			}
            }
    
        } // endfunc endElement
    
        function characterData( $parser, $data ){
            global $current_tag;
    
            if( $this->inside_item ){
                switch($current_tag){
    
                    case "TITLE":
                        $this->title .= $data;
                        break;
                    case "DESCRIPTION":
                        $this->description .= $data;
                        break;
                    case "LINK":
                        $this->link .= $data;
                        break;
    
                    default:
                        break;
    
                } // endswitch
    
            } // end if
    
        } // endfunc characterData
    
        function parse_results( $xml_parser, $rss_parser, $file )   {
    
            xml_set_object( $xml_parser, &$rss_parser );
            xml_set_element_handler( $xml_parser, "startElement", "endElement" );
            xml_set_character_data_handler( $xml_parser, "characterData" );
    
            $fp = fopen("$file","r") or die( "Error reading XML file, $file" );
    
            while ($data = fread($fp, 4096) and !$this->stop )    {
           
                // parse the data
                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 ) ) );
    		   
            } // endwhile
    
            fclose($fp);
    
            xml_parser_free( $xml_parser );
    
        } // endfunc parse_results
    
    
    } // endclass RSSParser
    
    global $rss_url;
    
    // Set a default feed
    if( $rss_url == "" )
        $rss_url = "http://www.casinogamblingweb.com/gambling.xml";
    
    $xml_parser = xml_parser_create();
    $rss_parser = new RSSParser(4);
    
    $rss_parser->parse_results( $xml_parser, &$rss_parser, $rss_url );
    ?>
    
    PHP:
     
    krakjoe, May 25, 2008 IP
  3. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #3
    thanks, thats great :)

    what did you change/add?

    I know this is very cheeky but could you also have a look at this for me please. I am currently getting spam attacked through my contact form and I don't know how to add a spam question. I just need something like "what is 1+seven"?

    thanks

    <?php
      $name = $_REQUEST['name'] ;
      $email = $_REQUEST['email'] ;
      $message = $_REQUEST['message'] ;
    
    
      if (!isset($_REQUEST['email'])) {
        header( "Location: http://www.site/contact.php" );
      }
      elseif (empty($email) || empty($message)) {
       
       header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
        header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
        header( "Cache-Control: no-cache, must-revalidate" );
        header( "Pragma: no-cache" );
    
    ?>
    
        <html>
        <head><title>Error</title></head>
        <body>
        <h1>Error</h1>
        <p>
        Oops, it appears you forgot to enter either your
        email address or your message. Please press the BACK
        button in your browser and try again.
        </p>
        </body>
        </html>
    
       <?php
    
    
    
      }
      else {
        mail( "email address", "title",
          "$message", "From: $name <$email>" );
        header( "Location: http://www.site.com" );
      }
    ?>
    PHP:
     
    fadetoblack22, May 29, 2008 IP