Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting media:thumbnail from XML

I just can't seem to be able to solve this. I want to get the media:thumbnail from an RSS file (http://feeds.bbci.co.uk/news/rss.xml).

I did some research and tried to incorporate insights from https://stackoverflow.com/questions/6707315/getting-xml-attribute-from-mediathumbnail-in-bbc-rss-feed and from other sources.

This is what I got:

$source_link = "http://feeds.bbci.co.uk/news/rss.xml";
$source_xml = simplexml_load_file($source_link);
$namespace = "http://search.yahoo.com/mrss/";

foreach ($source_xml->channel->item as $rss) {
    $title          = $rss->title;
    $description    = $rss->description;
    $link           = $rss->link;
    $date_raw       = $rss->pubDate;
    $date           = date("Y-m-j G:i:s", strtotime($date_raw));
    $image          = $rss->attributes($namespace);

    print_r($image);
}

When I run the script, all I see is a white page. If I echo or print_r any of the other variables, then it works like a charm. It's just the $image one which poses problems. Why isn't this working? Thx for any help!

like image 680
Tomi Seus Avatar asked Aug 17 '26 00:08

Tomi Seus


1 Answers

OK, it works now. I replaced

$image = $rss->attributes($namespace); 

with

$image = $rss->children($namespace)->thumbnail[1]->attributes();
$image_link = $image['url'];

and it works like a charm now.

like image 154
Tomi Seus Avatar answered Aug 18 '26 15:08

Tomi Seus