A WordPress Twitter stream, without the RSS…

Recently, Twitter made some changes and dropped support for RSS (as well as imposing plenty of other measures and limitations along the way). You can read about it here – http://mashable.com/2012/09/05/twitter-api-rss/

A while back, I shared some code on How to get a Twitter feed into your WordPress theme. These new changes to Twitter’s API mean that this method no longer works. So, I’ve updated it.

I’ve also moved it into a Class. This needs updating already, as the JSON API actually gives us access to all sorts of new things, but this should be a good starting point.

// Twitter Class
// Usage:
// $twitter = new Twitter;
// $twitter->twitter_messages();
class Twitter{
 
  function __construct() {
    $this->Twitter();
  }
 
  function Twitter(){
 
    define('MAGPIE_CACHE_ON', 1); //2.7 Cache Bug
    define('MAGPIE_CACHE_AGE', 1800);
    define('MAGPIE_INPUT_ENCODING', 'UTF-8');
    define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
 
  }
 
  public function twitter_messages($username = 'douglasradburn', $amount = 5, $error = 'Unable to retrieve tweets at this time.')
  {
 
    try {
      $feedDetails = $this->curlGet('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=' .  $username . '&&count=' . $amount);
      $feed = json_decode($feedDetails);
      $i=0;
      foreach($feed as $item){
      printf('<li>
      %s
      <span><a href="https://twitter.com/' . $username . '/status/%s">%s</a></span>
      </li>',$this->twitterify(strip_tags(str_replace($username.': ', '', $item->text))), $item->id, contextualTime(strtotime($item->created_at)));
 
      $i++;
      }
    } catch (Exception $exception) {
      echo $error;
    }
 
  }
 
  private function twitterify($ret) {
    # thanks to http://www.snipe.net/2009/09/php-twitter-clickable-links/#axzz0psFrJPHB
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2", $ret);
    $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
    $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
    return $ret;
  }
 
  private function curlGet($url)
  {
 
    // is cURL installed yet?
    if (!function_exists('curl_init')){
      echo 'Sorry cURL is not installed!';
      return;
    }
 
    $ch = curl_init(); // the target
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the page
    curl_setopt($ch, CURLOPT_URL, $url);
 
    $data = curl_exec($ch);
    curl_close($ch);
 
    return $data;
 
  }
 
}


You'd usually find a comments form here, but this post is too old to have anyone adding comments willy-nilly. You can always contact me if you have a comment.