richellyitalo
8/7/2019 - 1:40 AM

Using PHP's SimpleXML class to find the attributes of a <media:content> element in an MRSS file.

Using PHP's SimpleXML class to find the attributes of a <media:content> element in an MRSS file.

<?php

$mrss =<<<EOS
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"
  xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

  <channel>
    <title>My Movie Review Site</title>
    <link>http://www.foo.com</link>
    <description>I review movies.</description>
    <item>
      <title>Movie Title: Is this a good movie?</title>
      <link>http://www.foo.com/item1.htm</link>
      <media:content url="http://www.foo.com/trailer.mov" fileSize="12216320" type="video/quicktime" expression="sample" />
      <creativeCommons:license>
        http://www.creativecommons.org/licenses/by-nc/1.0
      </creativeCommons:license>
      <media:rating>nonadult</media:rating>
    </item>
  </channel>
</rss>
EOS;

$xml = new SimpleXMLElement($mrss);

$content = $xml->channel->item->children('media', true)->content;

$contentattr = $content->attributes();

foreach($contentattr as $name => $value){
    echo $name . ' = '.  $value . '<br />';
}
?>