Home > Software engineering >  How to get an element from RSS with PHP which hasn't an unique tag?
How to get an element from RSS with PHP which hasn't an unique tag?

Time:09-11

I have this RSS feed which has something like

<item>
<media:content url="https://blabla.jpg" type="image/jpeg" medium="image"></media:content>
<media:content url="https://blabla2.jpg" type="image/jpeg" medium="image"></media:content>
<item\>

Normally I would grab it like this:

$item->{'media:content'}->attributes()->url;

I would like to get the 2nd media content url. But because there are two of them it's not working. Is anyone willing to help a brother out? Thanks!

CodePudding user response:

How you're access the namespaced element won't work. You need the namespace URL to access them:

// Get media namespace uri
$mediaNS = $item->getNamespaces(true)['media'];
// Get content elements that are children of the item element
$contentElems = $item->children($mediaNS)->content;
// Get the last content element using array access and return the url attribute
$contentElems[$contentElems->count()-1]->attributes()->url;
  • Related