Zend - The PHP Company




XML

Add Code


RDF (RSS) Parser  

Type: code fragment
Added by: uncleozzy
Entered: 04/09/2000
Last modified: 08/12/2000
Rating: - (fewer than 3 votes)
Views: 14720

Turn your site into a pseudo-portal! This snippet lets you include news items from other websites that provide RDF / RSS files. You may, however, want to cache the RDF file locally, as downloading it every time the page is hit is not only slow, but also rude.

Includes function parseRDF() which takes a filename (which can be a URL) as its parameter. Outputs HTML links for each item in the RDF file. Can also do images. Will die if the file is invalid.



<?
    
    
#############################################
    #  This is rdf-parse.inc, which parses RDF  #
    #  files and outputs links to their ITEMs   #
    #                                           #
    #  Optionally, it can also print HTML for   #
    #  the IMAGE and CHANNEL bits of the XML.   #
    #  Just uncomment those lines in endElement #
    #  and put in appropriate HTML.             #
    #                                           #
    #  Usage:                                   #
    #  parseRDF("filename")                     #
    #   note: "filename" can be a URL           #
    #                                           #
    #  Requires:                                #
    #  PHP 4 compiled with "--use-xml"          #
    #                                           #
    #  Creates:                                 #
    #  Global variables $_depth, $_tags, and    #
    #  $_item                                   #
    #                                           #
    #  History:                                 #
    #  v0.6b - fr3nd notified me of a party-    #
    #          crashing feature of expat that   #
    #          didn't parse stuff like &amp;    #
    #          properly.  Fixed.  Also fixed    #
    #          initArray(), which didn't        #
    #          do anything at all.              #
    #  v0.61b- fixed a missing ) on line 46     #
    #############################################

        
$_item = array();
    
$_depth = array();
    
$_tags = array( "dummy" );
    
#  "dummy" prevents unecessary subtraction in the $_depth indexes

    
function initArray() {
           global 
$_item;

        
$_item = array ("TITLE"=>"""LINK"=>"""DESCRIPTION"=>"""URL"=>"" );
    }

    function 
startElement($parser$name$attrs) {
    global 
$_depth$_tags$_item;

        if ( (
$name=="ITEM")||($name=="CHANNEL")||($name=="IMAGE") ) {
            
initArray();
        }
        
$_depth[$parser]++;
            
array_push($_tags$name);
    }

    function 
endElement($parser$name) {
    global 
$_depth$_tags$_item;
    
        
array_pop($_tags);
        
$_depth[$parser]--;
        switch (
$name) {
            case 
"ITEM":
                echo 
"<P><A HREF="$_item[LINK]">$_item[TITLE]</A></P>n";
                                
initArray();
            break;

            case 
"IMAGE":
#                echo "<A HREF="$_item[LINK]"><IMG SRC="$_item[URL]" ALT="$_item[TITLE]" BORDER=0></A>n<BR>n";
                                
initArray();
            break;

            case 
"CHANNEL":
#                echo "<h3>$_item[TITLE]</h3>n";
                                
initArray();
                break;
        }
    }

    function 
parseData($parser$text) {
    global 
$_depth$_tags$_item;

        
$crap preg_replace("/s/"""$text);
        
# is the data just whitespace?
        # if so, we don't want it!

        
if ($crap) {
            
$text preg_replace("/^s+/"""$text);
            
# get rid of leading whitespace
                        
if ( $_item[$_tags[$_depth[$parser]]] ) {
                           
$_item[$_tags[$_depth[$parser]]] .= $text;
                        } else {
                           
$_item[$_tags[$_depth[$parser]]] = $text
                        }
        }
    }


    function 
parseRDF($file) {
    global 
$_depth$_tags$_item;

        
$xml_parser xml_parser_create();
        
initArray();

        
#  Set up event handlers
        
xml_set_element_handler($xml_parser"startElement""endElement");
        
xml_set_character_data_handler($xml_parser"parseData");

        
#  Open up the file
        
$fp fopen($file"r") or die( "Could not open $file for input");

        while (
$data fread($fp4096)) {
            if (!
xml_parse($xml_parser$datafeof($fp))) {
                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);
    }

?>


Usage Example


<?
   
include( "rdf-parse.inc" );
   
$rdffile "test.rdf";
   
$remote "http://freshmeat.net/backend/fm.rdf";
   
$refreshtime time() - 3600;
?>

<HTML>
<HEAD>
   <TITLE>RDF Showcase</TITLE>
</HEAD>

<BODY BGCOLOR=#FFFFFF>

<?
   
if ((filemtime($rdffile) < $refreshtime) || (filesize($rdffile) == 0)) {
     
$RDF fopen$rdffile"w" ) or die( "Cannot open $rdffile);
     
$FILE fopen$remote"r" ) or die( "Cannot open $remote);
     while (!
feof$FILE )) {
          
fwrite$RDFfgets$FILE1024 ));
     }
     
fclose$RDF );
     
fclose$FILE );
}

parseRDF$rdffile );

?>

</BODY>

</HTML>


Rate This Script





Search



This Category All Categories