Member Avatar for doctorphp

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.