
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





This may sound stupid but I’m a newbie to API programming but not a PHP or CodeIgniter newbie. I tried this API and have followed as per below :-
$this->load->library(’twitter’);
$this->twitter->auth(’user’, ‘pass’);
$timeline = $this->twitter->search(’trends’);
var_dump($timeline);
Now my question is that are all the calls to the API returned as a JSON object array ? Do I have to decode all the calls for the other functions as a JSON object ? Can anybody show some practical examples of a controller with a calls for this API which shows how to decode the JSON and also maybe passing it through the view.
Thanks.
Alijanah,
You don’t have to do any decoding. The function returns an object with properties based on the JSON field names. So if you do something like:
$userProfile = $this->twitter->call(’users/show’, array(’id’ => ‘elliothaughin’));
echo ($userProfile->description);
You should get “Kick-ass freelance web developer”
Now a question, on my development server at home the API works fine, I get the expect data returned. When I upload to my web server (on Dreamhost) the API calls don’t return anything. Curl is enabled on both, so I thinking maybe JSON isn’t working right at Dreamhost. Anyone seen this before?
[...] Twitter API CodeIgniter Library « Elliot Haughin – Published — 06/17/09 4:58 PM Similar Posts Fatal error: Call to undefined function similar_posts() in /nfs/c02/h09/mnt/29542/domains/emilolsson.com/html/system/wp-content/themes/emilolsson/single.php on line 108 [...]
My question is the same of alijanah.
Can anybody show some practical examples?
Tks.
Hi Elliot,
I am not able to get it worked on basic authentication using usernam and password. Could you help me out please…
This is my code..
$this->twitter = new Twitter();
$username = ‘xxxxxx’;
$password = ‘xxxxxx’;
$auth = $this->twitter->auth($username, $password);
$timeline = $this->twitter->call(’statuses/friends_timeline’);
Its returning null.
Thanks in advance
I am trying to use OAuth for a simple app, which works on my localhost but not on my production server (a VPS running CentOS 5) – the oauth_token is blank when the request is made to twitter.
I’ve tested cURL with PHP and that seems fine – is there anything else anyone would suggest?
Update – the problem was with json not being installed, as CentOS uses PHP 5.1, not 5.2 by default. So now my app can use oAuth and the tokens are returned successfully, however none of the functions do anything – lots of Nulls being returned, and I can’t update my status from the app either. Am I missing something?
I too think you should set up a github and let others contribute to this – it will be a fantastic library once complete!
Fot those with JSON encoding/decoding troubles, you should have a look there : http://abeautifulsite.net/notebook/71
But don’t forget you shouldn’t modify the Twitter class Elliot offers, so be smart
If it’s a cUrl issue, I’d advice you to purchase a new hosting account in a better place !
Thanks Elliot, this class kicks ass!
This seems like a silly problem to me, but I cannot for the life of me figure out how to output the search results. Any tips?
Hello,
I am using following call to send direct message but I am not getting any response from this call.
$res = $this->twitter->call(’direct_messages/new’, array(’user’ => ‘test’, ‘text’ => ‘This is a library test. Ignore’));
Anybody used this call?
I am using this call to send direct message
$this->twitter->call(’direct_messages/new’, array(’user’ => ‘nidaans’, ‘text’ => ‘This is a library test. Ignore’));
But not able to send direct message.
Anybody having solution?
Considering using this but I don’t see any prevention of exceeding API calls happening. Seems like on a production site with a ton of hits, 100 calls in an hour would happen almost instantly. Is this wrong?