2

I am attempting to get information from the yahoo music news xml file, found here:

http://news.yahoo.com/rss/music.

I have gotten everything, except I have no idea how to get the value of "url" from the "media:content" tag. Here is what I have so far:

<?php
    $newsxml = simplexml_load_file('http://news.yahoo.com/rss/music');
    foreach ($newsxml->channel->item as $newsstory){
         $newsimage = $newsstory['media']->content->url;
    }
?>

Any help in solving my problem would be greatly appreciated. Thanks!

MrCode
  • 63,975
  • 10
  • 90
  • 112
Eggo
  • 541
  • 7
  • 18
  • 1
    Refer http://php.net/manual/en/book.simplexml.php – Kiren S Jun 07 '13 at 04:58
  • Refer the examples for your answer – Kiren S Jun 07 '13 at 05:05
  • Strange, I used the examples and got "$newsimage = $newsstory->content->attributes()->url;" but it still does not seem to work. Do you have any idea what could be causing this problem? – Eggo Jun 07 '13 at 05:09
  • 1
    show me your xml file – Kiren S Jun 07 '13 at 05:16
  • Here is the link: http://news.yahoo.com/rss/music. Thanks for the help! – Eggo Jun 07 '13 at 05:17
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31370/discussion-between-kiren-siva-and-eggo) – Kiren S Jun 07 '13 at 05:42
  • People, once and for all: please POST YOUR XML in your question. Not the link. Why? 1. Your question on SO will stay, the linked content go away. Future users won't be able to learn from your question. 2. Many users can't open that link, e.g. from a mobile device etc. Don't exclude them from helping you. Thanks! – michi Jun 07 '13 at 09:59

2 Answers2

1

That's an XML namespace, so you need to pass the value of the media namespace to children() in order to access it.

$newsxml = simplexml_load_file('http://news.yahoo.com/rss/music');
foreach ($newsxml->channel->item as $newsstory){
     $newsimage = $newsstory->children('http://search.yahoo.com/mrss/')->content->attributes()->url;
}

The media namespace definition can be found at the top, as you can see it's defined as:

xmlns:media="http://search.yahoo.com/mrss/"

MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Use this code to loop through

$newsxml = simplexml_load_file('http://news.yahoo.com/rss/music');

print "<pre>";
print_r($newsxml->channel);
die;

And refer the link for getting the media elements

RSS XML Parsing issue (How to get media content value from the RSS feed?)

Here you can see in the

Community
  • 1
  • 1
Kiren S
  • 3,037
  • 7
  • 41
  • 69