Home > CakePHP, PHP > Tweet the content into Twitter

Tweet the content into Twitter

Following steps to create twitter apps In CakePHP :


Step 1 : Create a twitter A/C

Step 2 : CURL Must be enabled in your server

Step 3 : 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.,


Step 4 : Go ot Setting Tab in your apps, in Application type you must select the Read and Write or Read, Write and Access direct messages for your apps.

Step 5 : 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 library 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



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


$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 : To post content in your twitter A/C the following scripts used to post it

$msg = ‘The message which one you want for it’;

$update_status = $twitterObj->post_statusesUpdate(array(‘status’ => $msg));


$temp = $update_status->response;

// Update the status of the twitter object post the conetent and publish it


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

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: