Welcome Guest, Not a member yet? Register   Sign In
Show latest tweets
#1

[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.
#2

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

[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!
#4

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

[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.
#6

[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
#7

[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.
#8

[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!
#9

[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.
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB