CodeIgniter Forums
Show latest tweets - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Show latest tweets (/thread-23203.html)

Pages: 1 2


Show latest tweets - El Forum - 10-03-2009

[eluser]aryan_[/eluser]
I want to show 5 or 10 Twitter updates in my CI app. Please suggest a simple way to do this.

Thanks.


Show latest tweets - El Forum - 10-03-2009

[eluser]Berserk[/eluser]
http://www.haughin.com/code/twitter/


Show latest tweets - El Forum - 10-03-2009

[eluser]aryan_[/eluser]
already seen than, but looking for simpler solution just to show feeds.

btw, can you suggest a Twitter widget without twitter logo and username?

thanks!


Show latest tweets - El Forum - 10-03-2009

[eluser]Yorick Peterse[/eluser]
Code:
<?php
redirect('Location: http://www.twitter.com/yourusername');
?>



Show latest tweets - El Forum - 10-04-2009

[eluser]aryan_[/eluser]
[quote author="Yorick Peterse" date="1254627195"]
Code:
<?php
redirect('Location: http://www.twitter.com/yourusername');
?>
[/quote]

lol! This is not what I'm looking for.

I used SimpleXML library to parse Twitter RSS feed.


Show latest tweets - El Forum - 10-04-2009

[eluser]mvdg27[/eluser]
I've once used something like this. Not sure if it's the best way to go about it, but I needed a quick and dirty solution at the time.

Code:
function twitter_feed($user) {
    $rss_file = 'http://twitter.com/statuses/user_timeline/'.$user.'.xml';
    $rss_feed = @file_get_contents($rss_file);
        
    if($rss_feed) {
            
        $xml = simplexml_load_string($rss_feed);
        $return = array();
        foreach($xml as $status) {
            $return[] = array(
                'datetime' => $status->created_at,
                'text' => $status->text,
                'source' => $status->source
            );
        }

    }
    else $return = 'Twitter feed not found';

    return $return;
}

Guess this should get you started. Any suggestions on this code are very welcome of course.

Michiel


Show latest tweets - El Forum - 10-04-2009

[eluser]Unknown[/eluser]
I personally prefer to load the tweets on the client side, not on the server side. There are several nice jQuery Plugins for this issue, I use http://tweet.seaofclouds.com/.

At the beginning of my project I displayed the tweets via SimplePie on my CI page, but the problem was, that there was a great impact on the loading time of my page if twitter had some problems. With the client side solution, the page renders fast and the tweets are easily loaded without any side effects after displaying my main content.


Show latest tweets - El Forum - 10-05-2009

[eluser]Flemming[/eluser]
I like the jQuery solution but for what it's worth I'll post here the way I did it. I store the latest tweet in the DB, using a cron'd controller to fetch the tweet. This way, if twitter is down or slow, there is still the previous tweet stored in my DB. I guess it means a lot of database access just for the sake of getting a tweet!

One advantage is that you get access to all the data returned from Twitter and can manipulate it and display it as you wish. Another advantage(?) is that it doesn't require any javascript.

I've got to say though, the jQuery way is excellent if it suits your needs!


Show latest tweets - El Forum - 10-05-2009

[eluser]mvdg27[/eluser]
I have found some issues with twitter slowing the website down, but I prefer having all the data returned from Twitter such that I can manipulate it. One thing I've been thinking about is adding a caching mechanism. Which I think can be as simple as storing the xml feed in a file, and before accessing the live twitter feed, check whether the filetime is still valid. That would mean you only get a performance slowdown every x minutes, or hours, which I think is acceptable. The key is off course in figuring out a caching time that works for you.


Show latest tweets - El Forum - 10-05-2009

[eluser]mvdg27[/eluser]
I actually decided to enhance my old code a bit .. perhaps useful for others as well. Comments/ improvements are welcome

Code:
function twitter_feed($username = '', $max = 5, $cache_time = 3600) {

        $base_path = '/';
        
        // cache file and live file
        $cache_file = $base_path.'cache/twitter/'.$username.'.xml';
        $live_file = 'http://twitter.com/statuses/user_timeline/'.$username.'.xml';

        // check if a cache file exists        
        if(file_exists($cache_file)) {
            
            // is the cache file still valid?
            if(filemtime($cache_file) + $cache_time > mktime()) {
                $rss_feed = file_get_contents($cache_file);
            }
            // if not valid anymore
            else {
                // try and get the live feed
                $rss_feed = @file_get_contents($live_file);
                // save rss feed in cache file if the rss feed was retrieved successfully
                if($rss_feed) file_put_contents($cache_file, $rss_feed);
                // else use the cached file instead
                else $rss_feed = file_get_contents($cache_file);                    
            }
        }
        else {
            // try and get the live feed
            $rss_feed = @file_get_contents($live_file);
            // save rss feed in cache file if the rss feed was retrieved successfully
            if($rss_feed) file_put_contents($cache_file, $rss_feed);
            // else failed to retrieve feed, we can try again next time
            else $rss_feed = false;            
        }
        
        if($rss_feed) {
            
            $xml = simplexml_load_string($rss_feed);
            $i = 0;
            $return = array();
            
            foreach($xml as $status) {
            
                if($i >= $max) continue;
                $return[] = array(
                    'datetime' => $status->created_at,
                    'text' => $status->text,
                    'source' => $status->source
                );
                $i++;
            }


        }
        else $return = 'Twitter feed not found';
        
        return $return;    
    }

With this function you can retrieve your Twitter feed and cache the feed for a given period of time, to prevent your site from slowing down while constantly fetching the Twitter feed.