#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Posts
    113
    Rep Power
    0

    Displaying thumbnails from RSS feed


    Hello

    I'm trying to display a RSS feed and display the media:thumbnail image with it.

    I've got the feed working fine, but I'm struggling with displaying the thumbnails correctly. I'm using MTV news feed: http://www.mtv.co.uk/rss/news

    The code I'm using is:

    PHP Code:
    $url "http://www.mtv.co.uk/rss/news";
    $rss simplexml_load_file($url);

    $data $rss->xpath ('channel/item/media:thumbnail');


    if(
    $rss)
    {
    echo 
    '<h1>'.$rss->channel->title.'</h1>';
    echo 
    '<li>'.$rss->channel->pubDate.'</li>';
    $items $rss->channel->item;
    foreach(
    $items as $item)
    {
    $title $item->title;
    $link $item->link;
    $img $item->thumbnail;
    $published_on $item->pubDate;
    $description $item->description;

    echo 
    '<h3><a href="'.$link.'">'.$title.'</a></h3>';
    foreach (
    $data as $item) {
        
        echo 
    '<img src="'.$item['url'].'" />';
        
    }
    echo 
    '<span>('.$published_on.')</span>';
    echo 
    '<p>'.$description.'</p>';

    }


    If you look on http://www.teenchill.co.uk you'll see the problem. The feed displays correctly, but all 10 thumbnails are displayed rather than one for each news story.

    Can anyone point me in the right direction?

    Many thanks
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,992
    Rep Power
    9397
    PHP Code:
    $data $rss->xpath ('channel/item/media:thumbnail'); 
    That will get all the thumbnails. To get just the one from the current <item> change
    PHP Code:
    $img $item->children("media"true)->thumbnail
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Posts
    113
    Rep Power
    0
    Originally Posted by requinix
    PHP Code:
    $data $rss->xpath ('channel/item/media:thumbnail'); 
    That will get all the thumbnails. To get just the one from the current <item> change
    PHP Code:
    $img $item->children("media"true)->thumbnail
    Hello, thanks for the reply. I'm a little confused. Do I need to replace

    PHP Code:
    $data $rss->xpath ('channel/item/media:thumbnail'); 
    with
    PHP Code:
    $img $item->children("media"true)->thumbnail
    ?

    I've been messing around with this for a while now and only either manage to display all the images or none.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Posts
    113
    Rep Power
    0
    I think I have it figured out. I used:

    PHP Code:
    $thumbAttr $item->children('http://search.yahoo.com/mrss/')->thumbnail->attributes(); 
    Instead of

    PHP Code:
    $img $item->children("media"true)->thumbnail
    then echoed

    PHP Code:
    $thumbAttr['url'
    and that seems to have done the trick.

    Thanks for your help.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0
    Hello,

    I am trying to do something similar, could you please post your final code here?

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo