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

[eluser]Phil Sturgeon[/eluser]
It can be done at a VERY basic level following the explanation and code examples in this article. That shows you how to show a users feed and even support full syntax using plain PHP - so adjust accordingly for CodeIgniter.

It is obviously not the best method as it has no caching, no connection failure handling, etc but the point is to show how easy it can be to get a twitter feed on a page. :-)
#12

[eluser]mvdg27[/eluser]
Hi phill, I like your replacement code. There's one error in it though:

Code:
// Run all rules to replace syntax with HTML
$tweet->text = preg_replace(array_keys($patterns), array_values($replace), $tweet->text);

should be

Code:
// Run all rules to replace syntax with HTML
$tweet->text = preg_replace(array_keys($patterns), array_values($patterns), $tweet->text);

($replace should be $patterns)

Can I use your code to combine it with my function?
#13

[eluser]Phil Sturgeon[/eluser]
No, if you try using the code I posted on my blog I will hunt you down like a dog.

Actually... go for it. I am feeling kind today. :lol:
#14

[eluser]mvdg27[/eluser]
I'll take the risk of Phil changing his mind after all, and coming to hunt me down .. hehe Wink

Anyway, here is my latest version of the twitter function. I've added some functionality:

- timeout for retrieving the Twitter feed from twitter.com, so you can prevent your website from becoming too slow when Twitter is too busy
- parsing the Tweet text
- an option to force clearing the cache

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

        // pattern replacement rules to process the tweet texts (by Phil Sturgeon)
        $patterns = array(
            // Detect URL's
            '|([a-z]{3,9}://[a-z0-9-_./?&+]*)|i' => '<a href="$0" target="_blank">$0</a>',
            
            // Detect Email
            '|[a-z0-9._%+-][email protected][a-z0-9.-]+.[a-z]{2,6}|i' => '<a href="mailto:$0">$0</a>',
            
            // Detect Twitter @usernames
            '|@([a-z0-9-_]+)|i' => '<a href="http://twitter.com/$1" target="_blank">$0</a>',
            
            // Detect Twitter #tags
            '|#([a-z0-9-_]+)|i' => '<a href="http://twitter.com/search?q=#$1" target="_blank">$0</a>'
        );
        
        // cache file and live file
        $cache_file = '/cache/twitter/'.$username.'.xml';
        $live_file = 'http://twitter.com/statuses/user_timeline/'.$username.'.xml';

        // clear cache
        if($clear_cache) unlink($cache_file);

        // check if a cache file exists        
        if(file_exists($cache_file)) {
            
            // is the cache file still valid?
            if(filemtime($cache_file) + $cache_time > mktime()) {
                // read the rss feed from the cache
                $rss_feed = file_get_contents($cache_file);
            }
            // if not valid anymore
            else {
                // try and get the live feed
                $context = stream_context_create(array('http' => array('timeout' => $timeout)));
                $rss_feed = @file_get_contents($live_file, 0, $context);
                // 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);                    
            }
        }
        // cache file doesn't exist
        else {
            // try and get the live feed
            $context = stream_context_create(array('http' => array('timeout' => $timeout)));
            $rss_feed = @file_get_contents($live_file, 0, $context);
            // 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
            else $rss_feed = false;            
        }
        
        // Ok. We have an rss feed. Let's process it
        if($rss_feed) {
            // we use simplexml to create an object out of the rss feed
            $tweets = @simplexml_load_string($rss_feed);
            // simplexml couldn't process the rss feed properly
            if(!$tweets) {
                // we return an error
                $return = 'Twitter feed not found';
                // we clear the cache. No use for a cache file that can't be processed
                unlink($cache_file);
            }
            // yes. we have something to process
            else {
                // set our counter to '0'
                $i = 0;
                $return = array();
                // loop through the tweets
                foreach($tweets as $tweet) {
                    // check the counter
                    if($i >= $max) continue;
                    // add the tweet (text is being parsed)) to the return variable
                    $return[] = array(
                        'datetime' => $tweet->created_at,
                        'text' => preg_replace(array_keys($patterns), array_values($patterns), $tweet->text),
                        'source' => $tweet->source
                    );
                    // counter + 1
                    $i++;
                }
            }
        }
        // we ended up with no rss feed, return error
        else $return = 'Twitter feed not found';
        
        // return the result
        return $return;    
    }

Let me know what you think of the code, and how it works in a production environment.

Enjoy Smile

Michiel
#15

[eluser]aryan_[/eluser]
[quote author="Thollsten" date="1254740391"]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.[/quote]

Interesting! I'll try it.
#16

[eluser]aryan_[/eluser]
Thanks a lot! I'm using Simplepie library to parse feed.




Theme © iAndrew 2016 - Forum software by © MyBB