We dont just develop applications, we build business

Twitter Application in Cakephp

Recently I developed a cakephp based Twitter application for automating Twitter. The application LetUsFollow allows users to meet new people whom they can follow and in return they are awarded some credits. They can in turn offer some credits to others for following them. Thus the application lets users get free Twitter followers and grow their social network. The application also allows one to schedule tweets for future post. As there are not many decent tutorials over the net describing how to develop a Twitter app/mashup in cakephp, I decided to share a few code snippets so that someone can reuse them.

So to get started with our Twitter application, first register a new application on Twitter developer website. Give a suitable name to your Twitter application, some information about the purpose of the application in the description, address of the website where you want to host the app. Most fields are self-explanatory. The one to care about is the Callback URL. This is the url where Twitter should return to after successfully authenticating. More on this in coming paragraphs.

After setting up your application, note down your consumer key and secret. We will need this for authenticating any of our oauth calls. Now create a configuration file in your cakephp app/config directory named twitter.php and save your consumer key and secret :-

$config = array(
  'Tweets' => array(
    'consumer_key' => 'CONSUMER_KEY',
    'consumer_secret' => 'SECRET',
Now download this wonderful php sdk - Twitter-async which is a high performance wrapper for Twitter's OAuth API and provides parallel/asynchronous calls. It can be downloaded from here. Save the files EpiCurl.phpEpiOAuth.phpEpiTwitter.php inside your app/vendors directory. We shall import these files in our app_controller beforeFilter function :-
function beforeFilter(){
App::import('Vendor','EpiCurl',array('file' =>'EpiCurl.php'));
App::import('Vendor','EpiOAuth',array('file' =>'EpiOAuth.php'));
App::import('Vendor','EpiTwitter',array('file' =>'EpiTwitter.php'));
//Twitter configuration
$twitterConfig = Configure::read("Twitter");
$this->set('consumer_key', $twitterConfig['consumer_key']);
$this->set('consumer_secret', $twitterConfig['consumer_secret']);
$this->twitterObj = new EpiTwitter($twitterConfig['consumer_key'], $twitterConfig['consumer_secret']);
Note that we have initialized a twitter client object using our consumer key and secret. The next step is to create a controller for handling twitter authentication. We shall name it Twitters. Here is the controller code:-
class TwittersController extends AppController {
	var $name = 'Twitters';
	function beforeFilter(){
		$twitterId = $this->Auth->user('twitter_id');
		if(!isset($twitterId) || $twitterId == ''){
	function register(){
Needless to say, your users table must contain a field called twitter_id for storing the user's twitter id. In the beforeFilter function, we check if the user is logged in via Twitter i.e., whether twitter_id Auth variable is set or not. If not we redirect him to register action. Inside the register action we simply display him our Twitter authorization link :-
<?php echo '<a href="' . $twitterObj->getAuthorizationUrl() . '">Authorize with Twitter</a>'; ?>
When the user logins in with his Twitter account, he is redirected back to our callback url that we had set in the settings of our Twitter application. In my case, I wrote an action in my users controller to handle the Twitter registration. You must have guessed it by now that this action is the callback url of my Twitter application. Here is the code snippet:-

class UsersController extends AppController {

	var $name = 'Users';
	function beforeFilter(){

	function registerTwitterUser(){
		$token = $this->twitterObj->getAccessToken();
		$this->twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);
		$twitterInfo= $this->twitterObj->get_accountVerify_credentials();
		$data = array();
		$data['twitter_id'] = $twitterInfo->id;		
		$id = $this->Auth->user('id');
		if(!isset($id) || $id == ''){
			$count = $this->User->find('count',array('conditions' => array('twitter_id' => $data['twitter_id'])));
				$data = $this->User->find('first',array('conditions' => array('twitter_id' => $data['twitter_id'])));
			$data['id'] = $this->Auth->user('id');

The code is pretty simple. When the users authorizes our application with his twitter credentials, Twitter sends him back to this callback url with a signed request containing his oauth token. We then fetch the user's Twitter id and validate the user. In my case, I had facebook integrated too so the user might have already been logged in so I had to check whether he is a logged-in user and if so update the record with his Twitter id. If however he is a new user, I create a new record in the users table for this user and login in him.

You can see a live demo of this code in work here at LetUsFollow. Login in with your Twitter account and you can also get a few more targeted followers free :)

I hope this would be helpful for someone out there. Feel free to comment and suggest any code improvement.

You are here: Blogs Latest Twitter Application in Cakephp

facebook application development social applications joomla commerce business developers portal community software websites webappniche twitter network management requirement install drupal project services experience networking internet custom platform developer networks portals engines wordpress shopping customization android client optimization mobile ecommerce myspace companies windows joominvite virtuemart ferret develop installation opensocial google customers promote library service cakephp questions server quality technologies projects solutions travel company hosting providers professional traffic analysis apache solution maintenance programming linkedin framework marketing products