entries from an ATOM XML file. works with Blogger & MovableType atom files, haven't tried anything else. displays the results in the following format: mm/dd mm/dd is the month/day of the blog entry (and is optional), and is a hyperlink to the blog entry. invoked with following parameters: filename: full URL to the atom file to be parsed entries: number of entries to display show_date: if set to 1, will show the date in front of the hyperlink open_tag: any HTML tags to put in front of the entry, like
  • close_tag: any HTML tags to put at end of the entry, like <\li> examples you have to use %3c for <, %3e for > and %2f for \ (well, technically you don't need to use the %3c as < works) include ("http://www.theeislers.com/getlatest.php?entries=10&show_date=1&open_tags=&close_tags=%3cbr%2f%3e&filename=http://gadgetblog.theeislers.com/atom.xml"); displays the 10 most recent entries from gadgetblog.theeislers.com with the dates, and with a tag between each one include ("http://www.theeislers.com/getlatest.php?entries=5&show_date=0&open_tags=%3cli%3e&close_tags=%3c%2fli%3e&filename=http://gadgetblog.theeislers.com/atom.xml"); renders the 5 most recent entries from gadgetblog.theeislers.com (no dates), and puts each entry inside an
  • <\li> pair By Craig Eisler 11/27/05 You may copy and modify this code, but may not delete the attribution. ---------------------------------------------------------------- */ // // get the cmd line variables // reset($HTTP_GET_VARS); $szFileName = $HTTP_GET_VARS['filename']; $iEntries = (int) $HTTP_GET_VARS['entries']; $bShowDates = (int) $HTTP_GET_VARS['show_date']; $szOpenTags = $HTTP_GET_VARS['open_tags']; $szCloseTags = $HTTP_GET_VARS['close_tags']; // // initialize globals // if( $iEntries == 0 ) { $iEntries = 5; } $iNumEntries = 0; $bInEntryTag = false; $szTag = ""; $szTitle = ""; $szLink = ""; $szRel = ""; $szPublishedDate=""; // // startElementHandler // // callback function envoked at the start of an XML element. used to identify when we enter an "ENTRY" tag (so we can mine out the various bits of info // about the entry) and also to find the proper LINK entry (each entry has 2 LINK enries, an LINK to edit the blog entry and a LINK to the blog entry itself). // function startElementHandler($parser, $name, $attributes) { global $bInEntryTag, $szTag, $szTitle, $szPublishedDate, $szLink, $szRel, $iNumEntries, $iEntries; if( $bInEntryTag ) { $szTag = $name; // // special case for the LINK tag, we only want the one with the attribute REL="alternate", and the data is in the HREF attribute // if( $szTag == "LINK" ) { if( $attributes["REL"] == "alternate" ) { $szLink = $attributes["HREF"]; } } } elseif( $name == "ENTRY" ) { // // flag that we are inside an entry (don't process if we have already spewed the requested number of entries) // if( $iNumEntries < $iEntries ) { $bInEntryTag = true; } } } /* startElementHandler */ // // endElementHandler // // callback function envoked when an XML element is closed. used to identify when we are exiting the entry tag so we can spew out all the info we collected. // function endElementHandler($parser, $name) { global $bInEntryTag, $szTag, $szTitle, $szPublishedDate, $szLink, $szRel, $iNumEntries, $iEntries, $bShowDates, $szOpenTags, $szCloseTags; if( $name == "ENTRY" && $bInEntryTag ) { // // spew any opening tags // printf( "%s", $szOpenTags ); // // show the date if needed // if( $bShowDates == 1 ) { $dt = trim($szPublishedDate); $year = substr( $dt, 0, 4 ); $month = substr( $dt, 5, 2 ); $day = substr( $dt, 8, 2 ); printf("%s/%s ", $month, $day ); } // // hyperlink to blog entry // printf("%s", trim($szLink), htmlspecialchars(trim($szTitle)) ); // // spew any closing tags // printf( "%s", $szCloseTags ); // // new global variable state // $szTitle = ""; $szPublishedDate = ""; $szLink = ""; $szRel = ""; $bInEntryTag = false; $iNumEntries++; } } /* endElementHandler */ // // characterDataHandler // // callback envoked with data between an pair. used to extract the blog title and creation date // function characterDataHandler($parser, $data) { global $bInEntryTag, $szTag, $szTitle, $szPublishedDate, $szLink, $szRel, $iNumEntries, $iEntries; if( $bInEntryTag ) { switch( $szTag ) { case "TITLE": $szTitle .= $data; break; case "PUBLISHED": // movabletype uses PUBLISHED case "CREATED": // blogger uses CREATED $szPublishedDate .= $data; break; } } } /* characterDataHandler */ // // build a parser, and open the file // $xmlParser = xml_parser_create(); xml_set_element_handler( $xmlParser, "startElementHandler", "endElementHandler" ); xml_set_character_data_handler( $xmlParser, "characterDataHandler" ); $f = fopen( $szFileName,"r" ); if( $f == NULL ) { die( "Error opening $szFileName" ); } // // parse the data // while( $data = fread($f, 4096) ) { $rc = xml_parse( $xmlParser, $data, feof($f) ); if( !$rc ) { die( sprintf( "xml_parse error: %s on line %d", xml_error_string( xml_get_error_code( $xmlParser ) ), xml_get_current_line_number( $xmlParser ) ) ); } if( $iNumEntries >= $iEntries ) { break; } } fclose($f); xml_parser_free($xmlParser); ?>