SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Using SimplePie to take only first image from RSS feed.

    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->enable_cache(true);

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

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

    //init the process
    $feed->init();

    //let simplepie handle the content type (atom, RSS...)
    $feed->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>

    <h1>Ameena's Blog</h1>

    <?php foreach ($feed->get_items() as $item): ?>

    <h2><?php echo $item->get_title(); ?></h2>
    <h4><?php echo $item->get_date(); ?></h4>

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


    <?php endforeach; ?>

    </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. #2
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

    PHP Code:
    <item>    
    <
    title>PamiersQuand 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>Cliquertaper sur le claviervisionner les r&#233;sultats au tableau, c'est ce qui attend les élèves de l'école Cazalé cette année.  La mairie a financé l'achat du matériel informatique. Posé sur des...</description>    
    <pubDate>Tue14 Sep 2010 09:15:04 +0200</pubDate>    
    <
    lastBuildDate>Tue14 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érique, une première à 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. #3
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Micky,

    I think this is what we are looking for:

    http://simplepie.org/wiki/reference/.../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.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it seems to be working for me.

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

    PHP Code:
    Array
        (
        [
    0] => Array
            (
            [
    data] =>
            [
    attribs] => Array
                (
                [] => Array
                    (
                    [
    url] => 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épart de la 5e étape du Tour de l'avenir, Vals-les-Bains-Loriol-sur-Drôme. À 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 [URL] data contained in the array!

    The following does not work... what am I doing wrong?
    PHP Code:
                $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. #5
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I've got everything working... all by myself!

    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.

    PHP Code:
                $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. #6
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    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.

    Any help would be great.


  7. #7
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.



Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •