Integrating With Twitter API In CodeIgniter

ican 11 comments

I needed to integrate Twitter API in one  of my web applications which uses CodeIgniter framework.

 

As their is no direct library available in CI for twitter integration, at first go this might look very difficult, but it is really simple.

 

Here is how you can do it..

  1. First download the  Twitter class for PHP.
  2. Put the class.twitter.php file in application/libraries folder.
  3. Rename class.twitter.php to twitter.php ( to make it compatible with CI naming conventions).
  4. Open twitter.php file and set following variables
var $username=’'; //twitter username
var $password=''; // twitter password
var $user_agent=''; //an identifier for twitter preferably email address
Are you satisfied with your knowledge? No, then spent 15 minutes every day on PHPCamp.net a knowledge sharing website for our own PHP community

If you have followed these steps your integration is almost complete, now all you need to do is use the twitter library, here is an example to get you started 🙂

 $this->load->library('Twitter');
 $msg=’Just Integrated Twitter with CodeIgniter ’;
 $this->twitter->update($msg);

By the way you can follow me on twitter @thecancerus.

[/if.not_empty]

Comments

  1. david

    Instead of hardcoding the variables in the class add an initialize method that sets the variables for you and then you can use a config file with the same name to load the default values. But then you also have to possibility to create other twitter objects. $this->load->library(‘Twitter’); // loads class and adds config values $other_nest = new Twitter($other_config);

  2. david

    The main thing I was trying to express is the fact that adding values to the class itself makes the class inflexible. If you don’t want to change the class you can extend the class with your own using a prefix. This loads the parent class and the extended class into an object with the parent class name. A quick an dirty extended class class MY_Twitter extends Twitter { function MY_Twitter($settings) { parent::twitter(); $this->username = $settings[‘username’]; $this->password = $settings[‘password’]; $this->useragent = $settings[‘useragent’]; } }

  3. pete

    I’m a newbie in php,but I like your tutorial.How do you integrate twitpic api as a buddypress plugin which is a social wordpress MU CMS? www.buddypress.org

  4. nishantha

    Fatal error: Call to undefined function curl_init() in C:wampwwwCI1systemapplicationlibrariestwitter.php on line 713

  5. Sbudah

    Pursuant to David’s method – I simply expanded the main twitter class as follows: – Capitalize the class name to adhere to CI standards and added the following function after the variables declarations: /* * Authethicate the user – must be run after loading the library * @param array $settings contains user details (username, password and user_agent). */ function authenticate($settings){ $this->username = $settings[‘un’]; $this->password = $settings[‘pw’]; $this->user_agent = $settings[‘ua’]; return; } Usage guide on your controller: $settings = array(‘un’=>’username’, ‘pw’=>’password’, ‘ua’=>’user_agent’); $this->load->library(‘twitter’); $this->twitter->authenticate($settings);

  6. Tahsin Hasan

    hello, thanks for the post, you can find a twitter codeigniter library here on http://newdailyblog.blogspot.com/2010/07/download-tweetsigniter.html. Thanks.

  7. ali

    where is the twitter div..??

  8. hardik patel

    Notice: Undefined index: oauth_token in C:\xampp\htdocs\login_with_twitter_using_php\inc\twitteroauth.php on line 80 Notice: Undefined index: oauth_token_secret in C:\xampp\htdocs\login_with_twitter_using_php\inc\twitteroauth.php on line 80 Notice: Undefined index: oauth_token in C:\xampp\htdocs\login_with_twitter_using_php\process.php on line 53 Notice: Undefined index: oauth_token_secret in C:\xampp\htdocs\login_with_twitter_using_php\process.php on line 54

Leave a comment