Integrating With Twitter API In CodeIgniter

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.

11 thoughts on “Integrating With Twitter API In CodeIgniter

  1. 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);

    • thanks for suggestion david, i did not do any of those things as i did not wanted to change the original class file and my need was very well served.

  2. 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. Fatal error: Call to undefined function curl_init() in C:wampwwwCI1systemapplicationlibrariestwitter.php on line 713

  4. 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);

  5. 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

Comments are closed.