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();
- @raphaelleheaf oh well, I got totally wasted in Soho, would have been more fun with you :) (30 minutes ago)
- @jina looking forward to seeing you again in Oslo, time to write a talk :) (about 5 hours ago)
- @jbradforddillon also in a Futurama Episode :) (about 7 hours ago)
- Win: http://chzoddlyspecific.files.wordpress.com/2010/05/129182250720621140.jpg (about 8 hours ago)
- Hippozilla is watching you http://twitpic.com/2a0jsf (about 9 hours ago)
Customized to user and amount
include('./plain_twitter_badge.php');
plain_twitter_badge('philburns',10);
- Still on the road to #campbestival. Traffic awful, slow, loads of blydi tourists!!! (about 12 hours ago)
- Just finished packing the car for #campbestival. Sun coming up now, doubt we'll be first there!!! (about 19 hours ago)
- Printing off copies of the #campbestival map to laminate and attach to kids. (1 day ago)
- @LottieDean Did you have to pay to get the timings on the app? If so how much? (1 day ago)
- Fark! @campbestival app now demands a payment. But no price details. Hmmm (1 day ago)
- Deleted misbehaving @campbestival app, now it won't re-install. Grrrr!!! (1 day ago)
- http://twitpic.com/29jast - Mmmmmm #welshcakes! (2 days ago)
- @Laura_B_R indeed laura, not anny more!!! Hic! (2 days ago)
- @vicderbyshire Er @dianeforleader - sack your researcher Vic! (2 days ago)
- There is a bottle of wine too many in the fridge. Well, not for long!!! #whitewinewednesday (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)