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.

8 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);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>