Welcome to WebmasterWorld Guest from

Forum Moderators: bill & werty

Message Too Old, No Replies

Parseing the <enclosure> URL

How to extract a URL from the <enclosure> tag using magpie

9:06 pm on Jan 18, 2010 (gmt 0)

New User

5+ Year Member

joined:Jan 18, 2010
posts: 5
votes: 0

I was hopeing someone could help asap.

I am using Magpie RSS. I would like to extract the image URL from the <enclosure> tag in a RSS feed.

Here is the relevant source from the RSS feed that Magpie is parceing:

<enclosure url="http://www.example.com/myphoto.jpg" length="1" type="image/jpeg" />

Here is the relevant code snipit in my php file I am trying to use, to no avail:

echo '<img src="'.$item['enclosure'].'">';

What am I doing wrong?

Thanks in advance for any help!


3:52 pm on Jan 19, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
votes: 0

Welcome to WebmasterWorld. stipko!

When dealing with parsed XML feeds, I make liberal use of var_dump() [php.net] to figure out where the parser put everything.

In your case, try var_dump($item); to figure out the structure of the array/object. If it turns out Magpie parses XML into an object instead of an array, you have to use a different syntax to get the enclosure value:

echo '<img src="'.$item->enclosure.'">'; 
8:33 pm on Jan 19, 2010 (gmt 0)

New User

5+ Year Member

joined:Jan 18, 2010
posts: 5
votes: 0

Thanks for the response Gregg.

I tried the var_dump($item) to figure out the structure of the array/object and the result was this (I truncated some text for this forum):

array(10) {
string(16) "Avatar"
string(76) "http://www.example.com/avatar.html"
string(554) "This movie tops the box office"
string(23) "affiliates@example.com"
string(17) "Top 10 Box Office"
string(79) "http://www.example.com/avatar.html#comments"
string(29) "Mon, 18 Jan 2010 04:32:06 GMT"
string(37) "Example.com Top 10 Box Office Movies"
string(554) "This is the summary information"

The <enclosure...> wasn't referenced in the dump. So even though the image URL exists in the feed:

<enclosure url="http://www.example.com/myphoto.jpg" length="1" type="image/jpeg" />

...I still cant seem to extract it for use in my page. What am I doing wrong?

I am grateful for your time on this. I really appreciate it.


7:21 am on Jan 20, 2010 (gmt 0)


WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
votes: 99

Welcome stipko!

Way past my bedtime... I'm probably missing something here... ignore if irrelevant.

Just to be sure I've got the problem: when you look at the raw feed, you see the enclosure and the image URL, but for some reason you don't see it in the data as parsed by Magpie.

A few thoughts (as I say, perhaps ill-conceived due to late hour).

1. Based just on your variable name ($item), it looks to me like you're iterating through the $rss->items array and grabbing each as $item and then using var_dump() on that.

Have you checked for a $rss->images array? I think that's where Magpie puts images, but I might be wrong.

In any case, try a var_dump() for the full $rss object before running through it for the individual items.

[edit] not wrong I think
- [bblog.com...]
- [magpierss.sourceforge.net...]

2. Assuming you just can't get the image with Magpie, you have the feed XML right? Why not grab the feed itself grep for the enclosure and get the image that way?

I'm not sure how all the pieces of your app/script/site fit together so I'm not sure if #2 makes any sense or if that means you'll just be processing the feed twice, but it's a possibility.

3. Look into another RSS parser.