Welcome Guest, Not a member yet? Register   Sign In
Show latest tweets
#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.


Messages In This Thread
Show latest tweets - by El Forum - 10-03-2009, 01:11 PM
Show latest tweets - by El Forum - 10-03-2009, 02:00 PM
Show latest tweets - by El Forum - 10-03-2009, 02:05 PM
Show latest tweets - by El Forum - 10-03-2009, 04:33 PM
Show latest tweets - by El Forum - 10-04-2009, 05:14 AM
Show latest tweets - by El Forum - 10-04-2009, 04:13 PM
Show latest tweets - by El Forum - 10-04-2009, 11:59 PM
Show latest tweets - by El Forum - 10-05-2009, 05:46 AM
Show latest tweets - by El Forum - 10-05-2009, 05:56 AM
Show latest tweets - by El Forum - 10-05-2009, 07:59 AM
Show latest tweets - by El Forum - 10-05-2009, 08:09 AM
Show latest tweets - by El Forum - 10-05-2009, 09:00 AM
Show latest tweets - by El Forum - 10-05-2009, 09:58 AM
Show latest tweets - by El Forum - 10-05-2009, 10:22 AM
Show latest tweets - by El Forum - 10-05-2009, 02:15 PM
Show latest tweets - by El Forum - 10-05-2009, 02:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB