Twitter API for CodeIgniter

Twitter API for CodeIgniter

May 11

I came up with a random twitter app idea the other day, and during the excitement of this idea, I did some very sketchy research to see if there were any other similar services which offer a twitter group chat based on hashtags.

I didn’t find any that looked well developed and polished to be a threat. Figured the idea was very simple, and got building on it
After some googling, i found a great Twitter API library for CodeIgniter developed by Elliot Haughin.

The reason its awesome?

  1. Supports OAuth (I realized this will be Twitter’s main form of authentication moving forward – as they are removing basic auth in June 2010)
  2. Easy to setup
  3. Supports Search
  4. Output has been formatted to be closely aligned to what the API documentation states (example below will demonstrate)

This example will show how to present search query data.

These are a few lines in my controller to obtain search results

$this->load->library('twitter');
$key = 'somehashtag';
// this step is only required if you are doing a hashtag search, else you can omit
$key = '%23' . $key;
//the second param will accept an array of parameters to apply to the search query
$query= $this->twitter->search('search', array('q' => $key, 'rpp' => '100', 'result_type' => 'recent'));
foreach($query->results as $result) {
	$data['results'][] = $result;
}
$this->load->view('search', $data);

My view – Search.php

foreach($results as $row) {
	echo '<strong>' . $row->from_user . '</strong> said ' . $row->text . ' at <em>' . $row->created_at . '</em>';
}
?>

Notice how when you refer to the object $row in the view and $query in the controller, the associated values are mapped directly to the response data outlined in the API Documentation – this makes it super easy to play with.
The API also comes with an example controller which demonstrates how OAuth works and how to update your status, so be sure to check that out also.

After playing around with the library, i stumbled on this. It basically does exactly what I wanted my app to do. So i’ve dropped the ball on this project. Was fun while it lasted (a few hours)

Moral of the story: Do your research.

Leave a Reply