Home > CakePHP, PHP > Twitter Integration

Twitter Integration

Following steps to create twitter apps In CakePHP :

Step1 : Create a twitter A/C

Step2 : CURL Must be enabled in your server

Step3 : Log in to twitter A/C to create an API

a. https://dev.twitter.com/user/login?destination=home

b. Click username on top menu you have one sub menu named My applications

c. Click My application menu

d. Click Create a new application

e. Fill up the application details

f. Callback URL must have .php extenstion

g. Website must be a valid website. we can give our localhost url also

h. Callback URL means after verification of username and password the page redirect into which page

i. click the submit you get the API informations.

j. Under Details->OAuth settings we get Consumer key,Consumer secret and etc.,

Step4 : Download the twitter API from online

a. Go to http://www.jaisenmathai.com/articles/twitter-php-sign-in.html and click the link for available for download

b. UnZIP the downloaded package, place the librarie files into app/vendors/ folder create on folder twitter inside this folder create twitter.php file

c. In twitter.php we include the library files

include ‘EpiCurl.php’;

include ‘EpiOAuth.php’;

include ‘EpiTwitter.php’;

d. In app_controller top we include the following line, App::import(‘Vendor’, ‘twitter/twitter’);

e. Declare these variable as globally

var $consumer_key = ”;

var $consumer_secret = ”;

f. Create an Object for

$twitterObj = new EpiTwitter($this->consumer_key, $this->consumer_secret);

$twitterObj->getAuthenticateUrl() get the login url if user not login than

check whether session $this -> Session -> read(“oauth_token”) exists or not if its not exists than

$twitterObj = new EpiTwitter($this->consumer_key, $this->consumer_secret);

Object to connect the twitter API

$twitterObj->setToken($_GET[‘oauth_token’]);

twitter return a access token

$token = $twitterObj->getAccessToken();

$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);

set the Token into twitter object, the token is valid than we create a session for that token and secret

// save to cookies

if(isset($_GET[‘oauth_token’])){

$this -> Session -> write(“oauth_token”, $token->oauth_token);

$this -> Session -> write(“oauth_token_secret”, $token->oauth_token_secret);

}

$twitterInfo= $twitterObj->get_accountVerify_credentials();

Finally we get the twiter user A/C credentials

Check one more condition for session exists $this -> Session -> read(“oauth_token”). If its exists than, we create a Object for $twitterObj = new EpiTwitter($this->consumer_key, $this->consumer_secret, $this -> Session -> read(“oauth_token”), $this -> Session -> read(“oauth_token_secret”));

$twitterInfo= $twitterObj->get_accountVerify_credentials();

Now we get the user credentials.

Step 5 : When user can logout we destroy the session.

Advertisements
Categories: CakePHP, PHP
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: