mardi 5 mai 2015

How would I retrieve the thumbnail image that wordpress.com RSS generates using PHP?

I am trying to retrieve the image url that my wordpress.com RSS feed generates so I can include the image in the recent posts on my own website. I was able to use some code I found online to retrieve the other attributes but I cannot figure out how to get the image URL from the RSS.

The code I have is below:

RSS

<media:thumbnail url="http://ift.tt/1ckXpLQ"/>

PHP

<?php
$rss = new DOMDocument();
$rss->load('http://ift.tt/1DRL9Ib');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        'image' => $node->getElementsByTagName('media:thumbnail')->item(0)->nodeValue
        );
    array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    $image = $feed[$x]['image'];
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    //echo '<p>'.$description.'</p>';
}
?>

Aucun commentaire:

Enregistrer un commentaire