Parse RSS media:content php - Missing Attributes

Sparta

New Member
Hi I'm trying to build a simple rss reader that will also pull images if they exist. Here is my code so far - this will be a useful little script if I can get it to work!An XML snippet from my test feed - http://musicwithoutlabels.com/feed (this value will be dynamic):\[code\]<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Music Without Labels</title><atom:link href="http://musicwithoutlabels.com/feed/" rel="self" type="application/rss+xml"/><link>http://musicwithoutlabels.com</link><description>Be. Hear. Now.</description><lastBuildDate>Tue, 02 Oct 2012 18:03:54 +0000</lastBuildDate><language>en-US</language><sy:updatePeriod>hourly</sy:updatePeriod><sy:updateFrequency>1</sy:updateFrequency><generator>http://wordpress.org/?v=3.4</generator><item><title>Divine Fits Live At The Rock 11/02/12 [Live Music][New Music]</title><link>http://musicwithoutlabels.com/2012/10/01/divine-fits-live-at-the-rock-110212-live-musicnew-music/</link><comments>http://musicwithoutlabels.com/2012/10/01/divine-fits-live-at-the-rock-110212-live-musicnew-music/#comments</comments><pubDate>Mon, 01 Oct 2012 21:40:15 +0000</pubDate><dc:creator>Jordan Prather</dc:creator><category><![CDATA[Beat-Play, LLC]]></category><category><![CDATA[Events]]></category><category><![CDATA[Independent Music]]></category><category><![CDATA[Music]]></category><category><![CDATA[music industry]]></category><category><![CDATA[Music News]]></category><category><![CDATA[New Music]]></category><category><![CDATA[Photography]]></category><category><![CDATA[Uncategorized]]></category><category><![CDATA[Video]]></category><category><![CDATA[Arizona]]></category><category><![CDATA[Beat-Play]]></category><category><![CDATA[Britt Daniel]]></category><category><![CDATA[Dan Boeckner]]></category><category><![CDATA[Handsome Furs]]></category><category><![CDATA[Indie Music]]></category><category><![CDATA[Jordan Prather]]></category><category><![CDATA[live music]]></category><category><![CDATA[Music Without Labels]]></category><category><![CDATA[MusicWithoutLabels]]></category><category><![CDATA[Pop]]></category><category><![CDATA[rock]]></category><category><![CDATA[spoon]]></category><category><![CDATA[Tucson]]></category><guid isPermaLink="false">http://musicwithoutlabels.com/?p=30329</guid><description><![CDATA[Divine Fits is made up of two of my favorite current musicians. Dan Boeckner (Handsome Furs, Wolf Parade) and Britt Daniel of Spoon. While enjoying a Handsome Furs show, Britt Daniel joked to Dan that they should form a band together. Apparently that lighthearted comment stayed with the two of them, because Handsome Furs fans … <a class="more-link" href="http://musicwithoutlabels.com/2012/10/01/divine-fits-live-at-the-rock-110212-live-musicnew-music/">Continue reading</a>]]></description><wfw:commentRss>http://musicwithoutlabels.com/2012/10/01/divine-fits-live-at-the-rock-110212-live-musicnew-music/feed/</wfw:commentRss><slash:comments>0</slash:comments><media:content url="http://musicwithoutlabels.com/wp-content/uploads/2012/10/divinefit01-300x221.jpg" width="300" height="221" medium="image" type="image/jpeg"/> </item></channel></rss>\[/code\]And here is my parsing function:\[code\]function rssFeed($f) { $d = new DOMDocument(); # Create blank document $d->load($f); # Load contents of URL into document $a = array(); # Create empty array foreach ($d->getElementsByTagName('item') as $n) { # Here we declare all of our values $i = array('t' => $n->getElementsByTagName('title')->item(0)->nodeValue, 'd' => $n->getElementsByTagName('description')->item(0)->nodeValue, 'l' => $n->getElementsByTagName('link')->item(0)->nodeValue, 'p' => $n->getElementsByTagName('pubDate')->item(0)->nodeValue); array_push($a,$i); } foreach ($a as $p) { # Change this to handle the data however you would like echo '<p><a href="',$p['l'],'"><b>',$p['t'],'</b></a><br/>',$p['d'],'<hr/>',$p['p'],'</p>'; }}rssFeed('http://musicwithoutlabels.com/feed');\[/code\]That code works great, but if there's a picture I'd love to grab that - Here's what I've tried in order to get the 'url' attribute of "media:content" :\[code\]function rssFeed($f) { $d = new DOMDocument(); # Create blank document $d->load($f); # Load contents of URL into document $a = array(); # Create empty array foreach ($d->getElementsByTagName('item') as $n) { # Here we declare all of our values $i = array('t' => $n->getElementsByTagName('title')->item(0)->nodeValue, 'd' => $n->getElementsByTagName('description')->item(0)->nodeValue, 'l' => $n->getElementsByTagName('link')->item(0)->nodeValue, 'p' => $n->getElementsByTagName('pubDate')->item(0)->nodeValue, 'm' => $n->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content')->getAttribute('url'));array_push($a,$i);}foreach ($a as $p) { # Change this to handle the data however you would like echo '<img src="',$p['m'],'"/><p><a href="',$p['l'],'"><b>',$p['t'],'</b></a><br/>',$p['d'],'<hr/>',$p['p'],'</p>';}} rssFeed('http://musicwithoutlabels.com/feed');\[/code\]With this I get a 500 internal server errorI ran:\[code\]$theContent = @new SimpleXMLElement($xml);print_r($theContent);\[/code\]with $xml being the snippet of rss feed above, and I got this:\[code\]SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [title] => Music Without Labels [link] => http://musicwithoutlabels.com [description] => Be. Hear. Now. [lastBuildDate] => Tue, 02 Oct 2012 18:03:54 +0000 [language] => en-US [generator] => http://wordpress.org/?v=3.4 [item] => SimpleXMLElement Object ( [title] => Divine Fits Live At The Rock 11/02/12 [Live Music][New Music] [link] => http://musicwithoutlabels.com/2012/10/01/divine-fits-live-at-the-rock-110212-live-musicnew-music/ [comments] => http://musicwithoutlabels.com/2012/10/01/divine-fits-live-at-the-rock-110212-live-musicnew-music/#comments [pubDate] => Mon, 01 Oct 2012 21:40:15 +0000 [category] => Array ( [0] => SimpleXMLElement Object ( ) [1] => SimpleXMLElement Object ( ) [2] => SimpleXMLElement Object ( ) [3] => SimpleXMLElement Object ( ) [4] => SimpleXMLElement Object ( ) [5] => SimpleXMLElement Object ( ) [6] => SimpleXMLElement Object ( ) [7] => SimpleXMLElement Object ( ) [8] => SimpleXMLElement Object ( ) [9] => SimpleXMLElement Object ( ) [10] => SimpleXMLElement Object ( ) [11] => SimpleXMLElement Object ( ) [12] => SimpleXMLElement Object ( ) [13] => SimpleXMLElement Object ( ) [14] => SimpleXMLElement Object ( ) [15] => SimpleXMLElement Object ( ) [16] => SimpleXMLElement Object ( ) [17] => SimpleXMLElement Object ( ) [18] => SimpleXMLElement Object ( ) [19] => SimpleXMLElement Object ( ) [20] => SimpleXMLElement Object ( ) [21] => SimpleXMLElement Object ( ) [22] => SimpleXMLElement Object ( ) [23] => SimpleXMLElement Object ( ) ) [guid] => http://musicwithoutlabels.com/?p=30329 [description] => SimpleXMLElement Object ( ) ) ) )\[/code\]Not sure if that helps at all. Looks like some things may be missing..I'm really not too familiar w/ xml and rss yet though. Any help is GREATLY appreciated!!
 
Back
Top