Twitter recently stopped supporting simple authentication support which was the easiest way of integrating twitter integration in websites. Now it supports only token based authentication using OAuth.
From the web i selected some good articles(check articles referred at the end of document) and implemented.Let me quickly tell you the steps,

  1. Download OAuth from or download from here
  2. Create a folder twitter inside your web application and extract the OAuth files to twitteroauth.
  3. Add your application in Twitter ( ). Once you successfully register you can get consumer key and consumer secret.(Check the below image for details)
  4. Copy the CONSUMER_KEY and CONSUMER_SECRET to config.php file
    define('CONSUMER_KEY', 'KEY');
    define('CONSUMER_SECRET', 'SECRET');
    define('OAUTH_CALLBACK', '');
  5. Register your application with twitter using register.php file.
    require_once('config.php');$oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
    $request = $oauth->getRequestToken();
    $requestToken = $request['oauth_token'];
    $requestTokenSecret = $request['oauth_token_secret'];
    // place the generated request token/secret into local files
    file_put_contents('request_token', $requestToken);
    file_put_contents('request_token_secret', $requestTokenSecret);
    // display Twitter generated registration URL
    $registerURL = $oauth->getAuthorizeURL($request);
    echo '<a href="' . $registerURL . '">Register with Twitter</a>';

    Click on Register with Twitter link once you access register.php file http://websitename/path/twitter/register.php. Give Allow access to your application on twitter and then two files request_token and request_token_secret which will be used for generating access token and a PIN is generated which needs to be provided in validate.php

  6. Replace PIN_NUMBER in line number 20 with PIN generated during step 5 to 6 in validate.php.
    //Load required lib files.
    require_once('config.php');// Retrieve our previously generated request token &amp; secret
    $requestToken = file_get_contents("request_token");
    $requestTokenSecret = file_get_contents("request_token_secret");
    // Include class file &amp; create object passing request token/secret also
    $oauth = new TwitterOAuth(KEY, SECRET, $requestToken, $requestTokenSecret);
    // Generate access token by providing PIN for Twitter
    $request = $oauth->getAccessToken(NULL,PIN_NUMBER);
    $accessToken = $request['oauth_token'];
    $accessTokenSecret = $request['oauth_token_secret'];
    // Save our access token/secret
    file_put_contents("access_token", $accessToken);
    file_put_contents("access_token_secret", $accessTokenSecret);
  7. Now generate access tokens access_token and access_token_secret using validate.php using http://websitename/path/twitter/validate.php. These are the major files needed while interacting with twitter API using OAuth.
  8. Use the following code for quickly testing whether our tokens are valid.
    require_once('config.php');// Read in our saved access token/secret
    $accessToken = file_get_contents("access_token");
    $accessTokenSecret = file_get_contents("access_token_secret");
    // Create our twitter API object
    $oauth = new TwitterOAuth(KEY, SECRET, $accessToken, $accessTokenSecret);
    // Send an API request to verify credentials
    $credentials = $oauth->get("account/verify_credentials");
    echo var_dump($credentials);
    echo "Connected as @" . $credentials->screen_name;
    // Post our new "hello world" status
    $oauth->post('statuses/update', array('status' => "hello world"));

Check readme.txt for installation details in downloaded source.
Am using MySQL Database to store twitter updates in a table for a certain time period so that each time we don’t have to hit twitter server. In my example the index.php page fetches the tweets from specified twitter account and for posting data to twitter i have created a function pushToTwitter in pushtowitter.php.

For details regarding on more options on functionality and API please check referral articles.

Articles Referred:

Be Sociable, Share!