What is this then?
That one day, on Twitter, Phil Burns asked me about an accessible way to embed tweets in an HTML document. As all the official badges available use JavaScript and may be tricky to fix for screen readers and impossible to fix for people without JavaScript I had a go at a pure server-side solution and here we are.
Using the plain twitter badge
Using the plain twitter badge is as easy as including the PHP script and calling the plain_twitter_badge() function. The function has two optional parameters:
user- the Twitter user name
amount- the
amountnumber (alright, Alison) of tweets displayed
Styling the badge
The structure of the badge makes it pretty easy to style it to your needs:
<div class="plain-twitter-badge">
<div>
<a href="{url}">
<img src="{avatar}" alt="">{user name}
</a>
</div>
<ul>
<li>{tweet}</a> <span>{time ago}</span></li>
[...]
</ul>
</div>
Examples
Using presets
include('./plain_twitter_badge.php');
plain_twitter_badge();
- @shanselman I am drunk in Stockholm. Will check it on the morrow (about 8 hours ago)
- @dustyburwell because they are not css and should be stop gap solutions? (about 9 hours ago)
- So that is a vegetarian shepherds pie in Sweden. I feel like eating a baby. At least it costs a lot. http://t.co/B3Ibofb4 (about 13 hours ago)
- @hadleybeeman I especially like the "see more internet" link :) And that my name was only spelled wrong once:) (about 15 hours ago)
- RT @katskii: So.. its a dog.. dressed as 2 pirates carrying a treasure chest. I can't even fathom how brilliant this is. http://t.co/A4N0usmA (about 15 hours ago)
Customized to user and amount
include('./plain_twitter_badge.php');
plain_twitter_badge('philburns',10);
- @virginmedia #onceuponatime I dreamed of a free upgrade to 60mb broadband and new WiFi router..... *hopes* (16 minutes ago)
- @RobmDyson quality tune (18 minutes ago)
- RT @CardiffBluesSC: 75 followers before we hit 7,000. Thanks all for following! #cardiffblues #rugby (about 12 hours ago)
- @theboylatch this probably includes 'gifted and talented' as well as sen. (1 day ago)
- @RobdaBank Please don't forget we want @bdolansfr and @danlesac at campbestival..... (1 day ago)
- @BBCSport why won't there be a windows phone version of the Olympic app? (1 day ago)
- @WindowsPhoneUK why isn't there likely to be a version for windows phone? http://t.co/TgupiRxr (1 day ago)
- @alicma not likely to achieve either though! (2 days ago)
- @alicma fundraiser to pay for the clear up job needed for occupylondon ? (2 days ago)
- . @RevRichardColes is on Dave now. The channel that is. #HIGNFY #CommunardsReunion (2 days ago)
Source
<?php
/*
Plain twitter badge by Christian Heilmann
Version: 1.0
Homepage: http://isithackday.com/hacks/twitterbadge/
Copyright (c) 2009, Christian Heilmann
Code licensed under the BSD License:
http://wait-till-i.com/license.txt
*/
function plain_twitter_badge($user='codepo8',$amount=5){
$url = 'http://search.twitter.com/search.atom?q='.$user.'&rpp=30';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$updates = simplexml_load_string($output);
$ident = '';
$tweets = '';
$count = 0;
foreach($updates->entry as $d){
if($count === $amount){break;}
if($d->author->uri == 'http://twitter.com/'.$user){
if($ident === ''){
$url = $d->author->uri;
$name = $d->author->name;
$img = $d->link[1];
$img = $img->attributes();
$img = $img['href'];
$ident = '<div><a href="'.$url.'"><img src="'.$img.'" alt="">'.$name.'</a></div>';
}
$tweets .= '<li>' . $d->content . ' <span>(' .
distance_of_time_in_words(strtotime($d->published)) .
' ago)</span></li>';
$count++;
}
}
echo '<div class="plain-twitter-badge">'.$ident;
echo '<ul >' . $tweets . '</ul></div>';
}
/*
This is part of the date helper function of symfony:
(c) 2004-2006 Fabien Potencier
<fabien.potencier@symfony-project.com>
Thanks to Dustin Whittle for pointing it out
*/
function distance_of_time_in_words($from_time, $to_time = null,
$include_seconds = false){
$to_time = $to_time? $to_time: time();
$distance_in_minutes = floor(abs($to_time - $from_time) / 60);
$distance_in_seconds = floor(abs($to_time - $from_time));
$string = '';
$parameters = array();
if ($distance_in_minutes <= 1){
if (!$include_seconds){
$string = $distance_in_minutes == 0 ?
'less than a minute' : '1 minute';
}else{
if ($distance_in_seconds <= 5){
$string = 'less than 5 seconds';
}else if ($distance_in_seconds >= 6 && $distance_in_seconds <= 10){
$string = 'less than 10 seconds';
}else if ($distance_in_seconds >= 11 && $distance_in_seconds <= 20){
$string = 'less than 20 seconds';
}else if ($distance_in_seconds >= 21 && $distance_in_seconds <= 40){
$string = 'half a minute';
}else if ($distance_in_seconds >= 41 && $distance_in_seconds <= 59){
$string = 'less than a minute';
}else{
$string = '1 minute';
}
}
}
else if ($distance_in_minutes >= 2 && $distance_in_minutes <= 44){
$string = '%minutes% minutes';
$parameters['%minutes%'] = $distance_in_minutes;
}else if ($distance_in_minutes >= 45 && $distance_in_minutes <= 89){
$string = 'about 1 hour';
}else if ($distance_in_minutes >= 90 && $distance_in_minutes <= 1439){
$string = 'about %hours% hours';
$parameters['%hours%'] = round($distance_in_minutes / 60);
}else if ($distance_in_minutes >= 1440 && $distance_in_minutes <= 2879){
$string = '1 day';
}else if ($distance_in_minutes >= 2880 && $distance_in_minutes <= 43199){
$string = '%days% days';
$parameters['%days%'] = round($distance_in_minutes / 1440);
}else if ($distance_in_minutes >= 43200 && $distance_in_minutes <= 86399){
$string = 'about 1 month';
}else if ($distance_in_minutes >= 86400 &&
$distance_in_minutes <= 525959){
$string = '%months% months';
$parameters['%months%'] = round($distance_in_minutes / 43200);
}else if ($distance_in_minutes >= 525960 &&
$distance_in_minutes <= 1051919){
$string = 'about 1 year';
}else{
$string = 'over %years% years';
$parameters['%years%'] = floor($distance_in_minutes / 525960);
}
return strtr($string, $parameters);
}
?>
codepo8 (Christian Heilmann )
philburns (Phil Burns)