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();
- @snipeyhead how did that happen? (24 minutes ago)
- @rivalee I just got COVERED in the stuff on my way back! N4 ! (32 minutes ago)
- @segdeha http://t.co/UdisUNRF - bah, you use flash (33 minutes ago)
- @backgroundmedia http://t.co/UdisUNRF (34 minutes ago)
- Tablet computing has so much potential and all we do is touch the surface. (37 minutes ago)
Customized to user and amount
include('./plain_twitter_badge.php');
plain_twitter_badge('philburns',10);
- Did @rugbybc or @warkscc know about the snow tonight? There's a grit depot in dunchurch but none out on hillmorton rd #rugbytown #uksnow (about 1 hour ago)
- RT @MsLisaRogers: Cardiff fans to Blackpool "shit Barry Island, you're just a shit Barry Island" #CardiffCity (about 3 hours ago)
- @ajhmurray always is unfortunately. Inverdale and butler are awful at that part of the job. (about 3 hours ago)
- RT @gwladrugby: How French rugby has changed. It used to be cockerels on the pitch, now they can only afford pigeons (about 5 hours ago)
- Would be great to see Italy come back to steal this, but a 30pt margin for fr now looks more likely #6N12 #rugby (about 5 hours ago)
- @Laura_B_R when are you leaving #rugbytown? (about 11 hours ago)
- @gareththomas14 Are you fully retired now? I hear the blues have a gap at 15..... (1 day ago)
- @cathmcgoo c4 last night (1 day ago)
- Zut alors! #Chabal sacked by Racing #rugby #top14 http://t.co/l4SZKm62 (1 day ago)
- @rugbybc Wish I knew that last month! (1 day 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)