0

Hey everyone,

Parsing RSS is not my strong point and I am making a news feed for a client and they want me to use the Yahoo News RSS feed. I am using the following code.

<?php
$url = "http://news.yahoo.com/rss/europe";          
$xml = simplexml_load_file( $url );
?>
<h3 id="ln">Latest News</h3>

<?php
$newsCount = 0;
foreach ( $xml->channel->item as $item ) {

    ?>
    <h4><?php echo $item->title; ?></h4>
    <br />
    <?php 

    if( ++$newsCount == 3 ) {
        break;
    }

}

?>

I need to get this part of the feed.

<media:content url="http://l.yimg.com/bt/api/res/1.2/hzvrEk_IExvAFn.mR9tetQ--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/47cd94137a81a60b0d0f6a70670049cf.jpg" type="image/jpeg" width="130" height="86"/>

How do I incorporate this into the above code?

Thanks in advance.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by mschroeder
0

Since the media is part of a namespace you have to access that namespace to be able to access those nodes easily.

There are a few different ways for it to be achieved but I think this is the cleanest overall.

<?php

$feed = file_get_contents("http://news.yahoo.com/rss/europe");
$xml = new SimpleXmlElement($feed);

foreach ($xml->channel->item as $entry){
  $namespaces = $entry->getNameSpaces(true);
  $media = $entry->children($namespaces['media']); 
  echo $media->text;
  echo $media->credit;
}

Untested but you should get the general idea.

Edited by mschroeder

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.