Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 05:42:54 PM

Login with username, password and session length
Search:     Advanced search
Wollen Sie dem WebsiteBaker Team beitreten?
Nähere Informationen finden Sie unter hier und auf unserer neuen Webseite.
155544 Posts in 21714 Topics by 7736 Members
Latest Member: chris85
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: twitter-feed-droplet  (Read 921 times)
Ruud
WebsiteBaker Org e.V.

Offline Offline

Posts: 2297



WWW
« on: May 05, 2011, 10:51:25 PM »

Some time ago I needed to add a twitter feed to a website.
I did not want to use javascript for this because for SEO purposes it is good to have changing content in the page output.
So I made a Droplet for it.

Code:
<?php 
//use [[tweet?name=mytweetname&max=5]]

if (isset($_SESSION['tweets'])) return $_SESSION['tweets']; //prevent too many calls
if (!isset($name)) return true;
if (!isset(
$max)) $max 5;

if (!
function_exists('fetch_tweets')) {
function fetch_tweets($username$maxtweets) {
$tweets simplexml_load_file("http://twitter.com/statuses/user_timeline/" $username ".rss");
$tweet_array = array();  
foreach ( $tweets->channel->item as $tweet ) { 
if ($maxtweets == 0) {
break;
} else {
$twit $tweet->description;
$twit substr(strstr($twit': '), 2strlen($twit));
$twit preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$twit);
$twit preg_replace("(@([a-zA-Z0-9\_]+))""<a target=\"_blank\" href=\"http://www.twitter.com/\\1\">\\0</a>"$twit);
$twit preg_replace('/(^|\s)#(\w+)/''\1<a target=\"_blank\" href="http://search.twitter.com/search?q=%23\2">#\2</a>'$twit);
$twit iconv("UTF-8"DEFAULT_CHARSET$twit);
$pubdate strtotime($tweet->pubDate); 
$propertime date(DATE_FORMAT.', '.TIME_FORMAT$pubdate);  //Customize this to your liking
$tweet_item = array('desc' => $twit,'date' => $propertime,);
array_push($tweet_array$tweet_item);
$maxtweets--;
}
}
return $tweet_array;
}
}
$mytweets fetch_tweets($name$max);
$rval '<div class="twitter-updates">';
foreach (
$mytweets as $k => $v) {
     
$rval .= '<div><p class="twitter-update">' .$v['desc']. '</p><p class="twitter-date">' .$v['date']. '</p></div>';
}
$rval .= '</div>';
$_SESSION['tweets'] = $rval;
return 
$rval;

The droplet is called like [[tweets?name=billgates&max=10]] (max is optional)

It is depending on the simplexml library. According to the PHP website this is included in the standard PHP-5 installations, so it might work on most hosts.

The tweets are loaded once per browser session. This is to prevent being banned by twitter for requesting data too much from a single IP address.

Have fun with it.

Ruud
« Last Edit: May 05, 2011, 10:56:15 PM by Ruud » Logged

Professional WebsiteBaker Solutions
Bug

Offline Offline

Posts: 188


« Reply #1 on: May 08, 2011, 11:44:00 AM »

Sweet
Logged
Argos
Moderator
**
Offline Offline

Posts: 2161


WWW
« Reply #2 on: May 16, 2011, 02:50:31 PM »

This seems to be the same functionality as http://www.websitebakers.com/pages/modules/listings/various/wb-tweets.php, but then as a droplet. Is that correct?
Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
Ruud
WebsiteBaker Org e.V.

Offline Offline

Posts: 2297



WWW
« Reply #3 on: May 16, 2011, 03:05:13 PM »

in the end: yes. It shows tweets.  cool

It is very minimal, and because it is a droplet it is very easy to position and customize the output.
Logged

Professional WebsiteBaker Solutions
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!