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..
- First download the Twitter class for PHP.
- Put the class.twitter.php file in application/libraries folder.
- Rename class.twitter.php to twitter.php ( to make it compatible with CI naming conventions).
- 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.
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.
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’];
}
}
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?
http://www.buddypress.org
Fatal error: Call to undefined function curl_init() in C:wampwwwCI1systemapplicationlibrariestwitter.php on line 713
@nishantha You need to enable curl extension in php.ini
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);
hello,
thanks for the post, you can find a twitter codeigniter library here on http://newdailyblog.blogspot.com/2010/07/download-tweetsigniter.html. Thanks.
where is the twitter div..??
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
this type of error occur .give me a solution