Using SimplePie to take only first image from RSS feed


#1

Hi there,

I've had a search and I can't see a similar post, so I thought I'd start my own. I have recently discovered and started to use SimplePie to integrate feeds into websites. It's a great tool, and works well.

However, I now want to display just the first image from inside the content of a post with a caption, or short description. This is proving to be somewhat of a challenge for my naive brain.

Here's my code:

<?php

//get the simplepie library
require_once('inc/simplepie.inc');

//grab the feed
$feed = new SimplePie('http://ameenakaracallender.blogspot.com');

//enable caching
$feed-&gt;enable_cache(true);

//provide the caching folder
$feed-&gt;set_cache_location('cache');

//set the amount of seconds you want to cache the feed
$feed-&gt;set_cache_duration(1800);

//init the process
$feed-&gt;init();

//let simplepie handle the content type (atom, RSS...)
$feed-&gt;handle_content_type();

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RSS Feed Reader test page</title>
</head>

<body>

&lt;h1&gt;Ameena's Blog&lt;/h1&gt;

&lt;?php foreach ($feed-&gt;get_items() as $item): ?&gt;

&lt;h2&gt;&lt;?php echo $item-&gt;get_title(); ?&gt;&lt;/h2&gt;
&lt;h4&gt;&lt;?php echo $item-&gt;get_date(); ?&gt;&lt;/h4&gt;

&lt;?php echo '&lt;img src="' .$item-&gt;get_first_image_url(). '"/&gt;'; ?&gt;


&lt;?php endforeach; ?&gt;

</body>
</html>

The get_first_image_url command, is obviously pseudocode, and I have no idea how to create that function, but essentially that's what I need.

Any help would be great.

Many thanks in advance,
Micky


#2

Hi all,

I found this thread while searching a solution for the same problem.

In the particular feed I'm trying to get the image from, the URL is in the <enclosure> tag.

Micky, I'm guessing this might not always be the case and people might need to have a closer look at your RSS feed to give you an accurate answer?

<item>    
<title>Pamiers. Quand la souris remplace la craie</title>
<link>http://www.ladepeche.fr/article/2010/09/14/906353-Pamiers-Quand-la-souris-remplace-la-craie.html</link>
<copyright>DDM</copyright>    
<description>Cliquer, taper sur le clavier, visionner les r&#233;sultats au tableau, c'est ce qui attend les &#233;l&#232;ves de l'&#233;cole Cazal&#233; cette ann&#233;e.  La mairie a financ&#233; l'achat du mat&#233;riel informatique. Pos&#233; sur des...</description>    
<pubDate>Tue, 14 Sep 2010 09:15:04 +0200</pubDate>    
<lastBuildDate>Tue, 14 Sep 2010 09:15:04 +0200</lastBuildDate>
<guid isPermaLink="false">http://www.ladepeche.fr/article/2010/09/14/906353-Pamiers-Quand-la-souris-remplace-la-craie.html</guid>   <author>Marie Seurin.</author>
<enclosure url="http://www.ladepeche.fr/content/photo/biz/2010/09/14/201009141931_b100.jpg" type="image/jpeg" length="" title="La classe num&#233;rique, une premi&#232;re &#224; Pamiers./ Photo DDM Marie Seurin." provider="DDM" creator=""></enclosure>
</item>

How can we target a tag and get the data from it using simplepie? Is this the way to go or is there a better way?

Any help is much appreciated.


#3

Micky,

I think this is what we are looking for:

http://simplepie.org/wiki/reference/simplepie_item/get_item_tags

It should allow us to target the tag containing the data we are looking for.

I'll check on this on see what I can come up with and report here. wink


#4

Well, it seems to be working for me.

Here is the array I get back from calling get_item_tags()

Array
    (
    [0] => Array
        (
        [data] =>
        [attribs] => Array
            (
            [] => Array
                (
                 => http://www.ladepeche.fr/content/photo/biz/2010/09/14/201009142210_b100.jpg
                [type] => image/jpeg
                [length] =>
                [title] => Yoann Barbas (&#224; gauche) lors du d&#233;part de la 5e &#233;tape du Tour de l'avenir, Vals-les-Bains-Loriol-sur-Dr&#244;me. &#192; droite, le maillot jaune, Jannick Eijssen, vainqueur de la Ronde de l'isard 2010/DDM, Angel Cavicchiolo.
                [provider] => DDM
                [creator] =>
                )
            )
            [xml_base] =>
            [xml_base_explicit] =>
            [xml_lang] =>
        )
    )

Unfortunately I do not know enough about PHP to access the data contained in the array! :rofl:

The following does not work... what am I doing wrong?

            $file = $media_content[0]['attribs']['']['url'];
            echo $file;

I'll have a look at the PHP manual and see if I can figure this out.


#5

Ok, I've got everything working... all by myself! smiley

I used get_item_tags() to target the tag that contained the info I needed (link to the image) wich returned an array from wich I extracted just what I needed.

            $media_group = $item->get_item_tags('', 'enclosure');
            $file = $media_group[0]['attribs']['']['url'];
            echo $file;

It finally is fairly simple and someone with more than a couple of hours experience with PHP will certainly have no troubles getting this to work.

Hoping this is going to help you Micky.

See you!


#6

That's great cyberbrain! Big up yourself!

I really haven't got a clue when it comes to php, and until I get a chance to sit down and play around with it properly I'm totally at the mercy of others. Luckily however, a friend of mine wrote me this snippet of code to do the job, and it works perfectly.

function get_first_image_url($html)
{
if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
return $matches[1];
}
else return 'url_of_default_image_if_post_has_no_img_tags.jpg';
}

I then add the line:

<?php echo '<img src="' .get_first_image_url($item->get_content()). '"/>'; ?>

into my HTML code and voila it works a treat. Don't understand fully how it works, but I'm working on that. smile

I do have another problem now tho, which you may be able to shed some light on. I'd like to print out the start of the content of the post, but only the first sentence. Completely at a loss on that... I guess I need to parse the XML document up until the first '.', then put that into a new string, something like $first_sentence, then i can echo $first_sentence... Yeah, that's about as far as I've gotten. :lol:

Any help would be great.

wink


#7

So I was inspired by your array idea, and this is what I came up with:

//turn the $item->get_content into an array, using "." as a break point.
$sentence = explode(".",$item->get_content());

//print out the first array, being the first sentence.
echo $sentence[0];

Works a treat, but the full-stop is lost, so you have to put it back in like so:

echo $sentence[0]. '.';

Jobs a goodun. Thanks for the help.

smile