Professional Freelance Web Developer
CodeIgniter Activist, Caffeine Junkie

Elliot Haughin

Twitter API CodeIgniter Library

Twitter
Welcome to the fancy-new version of the Twitter API CodeIgniter Library.
How do you like this for simplicity?

This page is currently being updated for oAuth functions

Important!

To make this work, you will need to make a couple of crucial changes to your application/config/config.php

Open up your application/config/config.php file. Scroll down to the uri_protocol option and change it to ‘PATH_INFO’. Then go to the uri_allowed_chars setting and place a question mark ‘?’ after ‘a-z’. This allows you to put question marks in the URL. That’s it!

Once you’ve done this, you can drop the two libraries (MY_Input.php and Twitter.php) into you application/libraries/ directory.

Bundled with the release is some sample controller code. This code uses sessions to store the all-important access_token and access_token_secret. But, you could use a database for this.

Using basic authentication

$this->load->library('twitter');
$this->twitter->auth('username', 'password');
$timeline = $this->twitter->call('statuses', 'friends_timeline');

Using oauth authentication

To use auth, you will need to register an oAuth Client here.
Make sure you point the ‘callback url’ to the url of the controller code provided in the library.

Now you can use oauth to your heart’s content, just use the example controller code.

This library provides an interface to Twitter’s beautiful API. What’s more – it tries to stick as close to the original specification as possible. So, all you need is the ‘Twitter Api Documentation‘, and you should be good to go! This library also handles Twitter’s search API (see examples below).

Here’s some examples of more ‘complex’ (yeah right!) calls:

Updating your status:

$this->twitter->call('statuses', 'update', array('status' => 'Elliot Rocks!'));

Get the third page of someone else’s followers:

$this->twitter->call('users', 'followers', array('id' => 'dallard', 'page' => 3));

Use the search method

$this->twitter->search('search', array('q' => 'haughin'));

Trending topics

$this->twitter->search('trends');

Full list of calls available

$this->twitter->search('search', array('q' => 'elliot'));
$this->twitter->search('trends');
$this->twitter->search('trends/current');
$this->twitter->search('trends/daily');
$this->twitter->search('trends/weekly');
$this->twitter->call('statuses/public_timeline');
$this->twitter->call('statuses/friends_timeline');
$this->twitter->call('statuses/user_timeline');
$this->twitter->call('statuses/show', array('id' => 1234));
$this->twitter->call('direct_messages');
$this->twitter->call('statuses/update', array('status' => 'If this tweet appears, oAuth is working!'));
$this->twitter->call('statuses/destroy', array('id' => 1234));
$this->twitter->call('users/show', array('id' => 'elliothaughin'));
$this->twitter->call('statuses/friends', array('id' => 'elliothaughin'));
$this->twitter->call('statuses/followers', array('id' => 'elliothaughin'));
$this->twitter->call('direct_messages');
$this->twitter->call('direct_messages/sent');
$this->twitter->call('direct_messages/new', array('user' => 'jamierumbelow', 'text' => 'This is a library test. Ignore'));
$this->twitter->call('direct_messages/destroy', array('id' => 123));
$this->twitter->call('friendships/create', array('id' => 'elliothaughin'));
$this->twitter->call('friendships/destroy', array('id' => 123));
$this->twitter->call('friendships/exists', array('user_a' => 'elliothaughin', 'user_b' => 'jamierumbelow'));
$this->twitter->call('account/verify_credentials');
$this->twitter->call('account/rate_limit_status');
$this->twitter->call('account/rate_limit_status');
$this->twitter->call('account/update_delivery_device', array('device' => 'none'));
$this->twitter->call('account/update_profile_colors', array('profile_text_color' => '666666'));
$this->twitter->call('help/test');

REQUIREMENTS:

  • php5
  • curl
  • json_decode

TODO:

  • Profile Image Uploads
  • Background Image Uploads
  • Curl Cookie based sessions? – Maybe

Download Twitter API CodeIgniter Library

License: GNU GENERAL PUBLIC LICENSE – Version 2
Version: 3.1

Update: Version 3.1 (2009-05-01): Fixed bug when calling method with ‘/’ in name.

Update: Version 3.0 (2009-05-01): Added oAuth. Complete rewrite, as noted in this post.

Update: Version 2.2 (2009-03-07): Fixed user_timeline bug found by sophistry

Boring Stuff

Design © copyright Elliot Haughin 2009

Content published here are copyright their respective owners.

You cannot copy content from this site, either in English or translated to another language.

Keep Subscribed

Theres lots of ways for you to keep up with me on the web.

Please Note

Information given out on this blog should only be used as a guideline. I hold no liability for any code I write.

Always consult a professional before acting on this guidance.