Plain Twitter badge - a server-side solution to display tweets

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);
          

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);
  }
?>

Creative Commons LicensePlain Twitter Badge by Christian Heilmann is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License. Based on a work at isithackday.com. The software parts are licensed with the BSD license.