Author Topic: twitter-feed-droplet  (Read 3389 times)

Offline Ruud

  • Posts: 3660
  • Gender: Male
  • Do not use PM for help! Please use the forum!
    • Dev4Me - Professional WebsiteBaker Development
twitter-feed-droplet
« on: May 05, 2011, 11: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: [Select]
<?php 
//use [[tweet?name=mytweetname&max=5]]

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

if (!
function_exists(&#39;fetch_tweets&#39;)) {
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, &#39;: &#39;), 2, strlen($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(&#39;/(^|\s)#(\w+)/&#39;, &#39;\1<a target=\"_blank\" href="http://search.twitter.com/search?q=%23\2">#\2</a>&#39;, $twit);
$twit iconv("UTF-8"DEFAULT_CHARSET$twit);
$pubdate strtotime($tweet->pubDate); 
$propertime date(DATE_FORMAT.&#39;, &#39;.TIME_FORMAT, $pubdate);  //Customize this to your liking
$tweet_item = array(&#39;desc&#39; => $twit,&#39;date&#39; => $propertime,);
array_push($tweet_array$tweet_item);
$maxtweets--;
}
}
return $tweet_array;
}
}
$mytweets fetch_tweets($name$max);
$rval = &#39;<div class="twitter-updates">&#39;;
foreach ($mytweets as $k => $v) {
     
$rval .= &#39;<div><p class="twitter-update">&#39; .$v[&#39;desc&#39;]. &#39;</p><p class="twitter-date">&#39; .$v[&#39;date&#39;]. &#39;</p></div>&#39;;
}
$rval .= &#39;</div>&#39;;
$_SESSION[&#39;tweets&#39;] = $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, 11:56:15 PM by Ruud »

Offline Bug

  • Posts: 237
Re: twitter-feed-droplet
« Reply #1 on: May 08, 2011, 12:44:00 PM »
Sweet

Offline Argos

  • Posts: 2542
  • Gender: Male
    • Argos Media
Re: twitter-feed-droplet
« Reply #2 on: May 16, 2011, 03: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?
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

Offline Ruud

  • Posts: 3660
  • Gender: Male
  • Do not use PM for help! Please use the forum!
    • Dev4Me - Professional WebsiteBaker Development
Re: twitter-feed-droplet
« Reply #3 on: May 16, 2011, 04:05:13 PM »
in the end: yes. It shows tweets.  8-)

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