Search code examples
phparrayscodeigniterrsssimplepie

using simplepie and codeigniter to list set icons next to specific feed types


I have a small issue which I'm hoping someone can help me resolve.

Copied below is a piece of code which goes away to my database model and returns an array with a URL and a feed type. The user enters whether they have used a twitter name, blogger site etc.

However, to parse this data with simplepie I need to pass an array of URL's... and I lose my "type" option. Is there anyway I could add this back to the simplepie item so that I can display a specific icon if the user has selected a specific type of feed.

$feed = $this->profile_model->get_module_feed();

    $feedarr = array();
    foreach($feed['data'] as $feeds) {
        if ($feeds['type'] == 1 ) {
            $i = "http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=" . $feeds['rss'];
        }
        if ($feeds['type'] == 2 ) {
            $i = "http://search.twitter.com/search.atom?q=" . $feeds['rss'];
        }
        if ($feeds['type'] >= 3 ) {
            $i = $feeds['rss'];
        }

        $d = $feeds['type'];

        //else $i = $feeds['rss'];

        echo $i . " " . $d . "<br />";

        array_push($feedarr, $i);
    }

    $this->simplepie->set_feed_url($feedarr);
    $this->simplepie->enable_cache(false);
    $this->simplepie->init();
    $this->simplepie->handle_content_type();


    $data = $this->simplepie;

    foreach($data->get_items() as $item) { ?>
        <h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
        <p><?php echo $item->get_description(); ?></p>
        <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
    <?php


    }


    }
}


?>

Eg, if the $feed['type'] is 1 I would like to display the image associated with 1

hoping someone can help as I'm a wee bit stumped!


Solution

  • You can use the individual feed item's feed property to get it's subscribe url and try to match it with the feed urls you used for the fetching. Here's a simplified example:

    // array with unique feed url => feed type (icon)
    $feed_types = array(
        'http://feed.one.com/' => 'type1',
        'http://feed.two.com/' => 'type2',
    );
    
    $pie = new SimplePie;
    $pie->set_feed_url(array_keys($feed_types));
    $pie->enable_cache(false);
    $pie->init();
    $pie->handle_content_type();
    
    foreach ($pie->get_items() as $item) {
        $feed_url = $item->feed->subscribe_url();
        $type = isset($feed_types[$feed_url]) ? $feed_types[$feed_url] : 'unknown type';
        print $type;
    }
    

    As long as you use the same url's for fetching that embedded into the feed results the subscribe_ul() method's result should align up with the urls used in set_feed_url().