When you look at the actual RSS file, you'll see the ENCLOSURE lines have something in them, but I'm still having problems digging it out of there. I searched around this board and apparently there are a few others who have battled the attribute fight as well. To date, I've only found one website that has some code that works, though my attempts to dupe some of his code into mine has led to failure.
I dumped the code I was using earlier because I too, couldn't figure out what some of it was doing. Here is what I'm operating off of now, and it works for most of the stuff I want, just not enclosures. I know my syntax for grabbing the attribute is messed, but I haven't figured it out just yet.
When I asked it to echo tagName, I'll get ENCLOSURE but when I run things, it looks like it isn't registering as being "TRUE". Ugh.
((By the way, this code worked once from here: http://www.ipportunities.nl/wordpress/index.php?m=20040824 but now I messed it up and have dropped it in favor of what I have below)).
<?php
// Global variables for function use.
$GLOBALS['title'] = false;
$GLOBALS['link'] = false;
$GLOBALS['description'] = false;
$GLOBALS['enclosure'] = false;
$GLOBALS['pubdate'] = false;
$GLOBALS['item'] = false;
$GLOBALS['titletext'] = null;
$GLOBALS['linktext'] = null;
$GLOBALS['desctext'] = null;
$GLOBALS['url'] = null;
$GLOBALS['enclosuretext'] = null;
$GLOBALS['pubdatetext'] = null;
$GLOBALS['enclosure_url']= null;
// function: startElement
// Deals with the starting element
function startElement( $parser, $tagName, $attrs='' ) {
// By setting global variable of tag name
// I can determine which tag I am currently
// parsing.
switch( $tagName ) {
case 'ITEM':
$GLOBALS['item'] = true;
break;
case 'TITLE':
$GLOBALS['title'] = true;
break;
case 'LINK':
$GLOBALS['link'] = true;
break;
case 'DESCRIPTION':
$GLOBALS['description'] = true;
break;
case 'PUBDATE':
$GLOBALS['pubdate'] = true;
break;
case 'ENCLOSURE':
$GLOBALS['enclosure'] = true;
break;
}
}
// function: endElement
// Deals with the ending element
function endElement( $parser, $tagName ) {
switch( $tagName ) {
case 'TITLE':
if( $GLOBALS['item'] == true ) {
echo $tagName;
echo ": <b>" . $GLOBALS['titletext'] . "</b><br/>";
}
$GLOBALS['title'] = false;
$GLOBALS['titletext'] = "";
break;
case 'LINK':
if( $GLOBALS['item'] == true ) {
echo $tagName;
echo ": <a href=\"". $linkurl . "\">View Article</a><br/>";
}
$GLOBALS['link'] = false;
$GLOBALS['linktext'] = "";
break;
case 'DESCRIPTION':
if( $GLOBALS['item'] == true ) {
echo $tagName;
echo": " . $GLOBALS['desctext'] . "<br/>";
}
$GLOBALS['description'] = false;
$GLOBALS['desctext'] = "";
break;
case 'PUBDATE':
if( $GLOBALS['item'] == true ) {
echo $tagName;
echo ": " . $GLOBALS['pubdatetext'] . "<br/>";
}
$GLOBALS['pubdate'] = false;
$GLOBALS['pubdatetext'] = "";
break;
case 'ENCLOSURE':
if( $GLOBALS['item'] == true ) {
echo $tagName;
echo ": " . $GLOBALS['enclosuretext'] . "<p/>";
}
$GLOBALS['enclosure'] = false;
$GLOBALS['enclosuretext'] = "Crap";
break;
}
}
// function: charElement
// Deals with the character elements (text)
function charElement( $parser, $text ) {
// Verify the tag that text belongs to.
// I set the global tag name to true
// when I am in that tag.
if( $GLOBALS['title'] == true ) {
$GLOBALS['titletext'] .= htmlspecialchars( trim($text) );
} else if( $GLOBALS['link'] == true ) {
$GLOBALS['linktext'] .= trim( $text );
} else if( $GLOBALS['description'] == true ) {
$GLOBALS['desctext'] .= htmlspecialchars( trim( $text ) );
} else if( $GLOBALS['pubdate'] == true ) {
$GLOBALS['pubdatetext'].= trim( $text) ;
} else if( $tagName == 'enclosure') {
$GLOBALS['ENCLOSURE'][$attrs['URL']] = $attrs['VALUE'];
$GLOBALS['enclosuretext'] .= $GLOBALS['ENCLOSURE'][$attrs['URL']] ;
// This is just crap above. It doesn't work.
}
}
// Create an xml parser
$xmlParser = xml_parser_create();
// Set up element handler
xml_set_element_handler( $xmlParser, "startElement", "endElement" );
// Set up character handler
xml_set_character_data_handler( $xmlParser, "charElement" );
// Open connection to RSS XML file for parsing.
$fp = fopen( "http://rasterweb.net/raster/feeds/rwaudio.rss", "r" )
or die( "Cannot read RSS data file." );
// Parse XML data from RSS file.
while( $data = fread( $fp, 4096 ) ) {
xml_parse( $xmlParser, $data, feof( $fp ) );
}
// Close file open handler
fclose( $fp );
// Free xml parser from memory
xml_parser_free( $xmlParser );
?>