0

I am trying to get recent posts from my blog's rss and print them through php, All I have done has worked fine but I am unable to get image thumbnail from the XML file. It is stored in an attribute.

My xml output is:

<item><title>Stackoverflow inspired movies list</title><link>http://www.example.com/2015/03/movies-list.html</link><author>(Ididntknewit)</author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://the.url.com/image.png' height='72' width='72'/><thr:total>0</thr:total></item>

My PHP Code:

<?php
header('Content-Type: text/html');

$rss =simplexml_load_file($requestURL);
?>
<?php
echo '<ul>';
foreach($rss->channel->item as $post){
    echo '<li>';
    $max = $post->media:thumbnail['url'];
    echo $max;
    echo '<a href="'.$post->link.'">'.$post->title.'</a>';
    echo '</li>';
}
echo '</ul>';
?>

How can I get the thumbnail URL.

IdidntKnewIt
  • 572
  • 6
  • 23

1 Answers1

0

Your main problem is that SimpleXML won't parse media:thumbnail. I wrote a quick solution for you, but I'm pretty sure there is a more efficient way of doing this:

header('Content-Type: text/html');

$rss = simplexml_load_string(
    // replace media:thumbnail with mediathumbnail
    str_replace(
        'media:thumbnail',
        'mediathumbnail',
        file_get_contents($requestURL)
    )
);

echo '<ul>';
foreach($rss->channel->item as $post)
{
    echo '<li>';
    // getting mediathumbnail attributes
    $attributes = $post->mediathumbnail->attributes();
    // this is the thumbnail url
    $max = $attributes->url;
    echo $max;
    echo '<a href="'.$post->link.'">'.$post->title.'</a>';
    echo '</li>';
}
echo '</ul>';

Hope I helped. :)