Tag Archives: php

PHP Twitter OAuth integration

This is a small how-to for integrating Twitter status updates into your website using PHP.

After succesfully using basic authentication for several months, twitter introduced a new authentication method Oauth as of August 31 2010.

This meant that i had to look for a solution to rewrite several lines of code to enable the website to post tweets using the new method. This is a small howto to integrate the new OAuth method using a quite old PHP 5 version.

Prerequisites

  • Basic programming skills
  • Web- and FTP server access
  • PHP 5.1.6+
  • PEAR JSON Class (if you’re not on PHP >=5.2.0)
  • Registered application at dev.twitter.com
  • Oauth token and Oauth secret
  • Abraham Williams’ PHP Oauth library

Installation

Well, let’s get started, first and most important thing to do is to let Twitter know that you’re creating an application that want’s to post tweets:

  1. Go to http://dev.twitter.com and make sure you’ve signed in by using your already existing twitter account
  2. Register a new app using the button on the upper righthand side of the page
  3. Make sure you fill all the required field with genuine information, specifically pay attention to the following fields/choices:
    1. Application Type -> Choose for ‘Browser’
    2. Callback URL -> Choose a path where you will install your php files; http://<your.tld>/<path>/callback.php
    3. Default Access type -> Choose for ‘Read & Write’
  4. Click Register and agree with the terms of serivce, your application is now registered!
  5. An important page is shown now, make sure you keep the listed details somewhere safe, for this howto you will need the following:
    1. Consumer key
    2. Consumer secret
    3. My Access Token (not listed directly, but can be viewed by clicking the ‘My Access Token’ on the right side of the page.

The application is now registered at dev.twitter.com, please make sure that you download the following files:

If you’re not on PHP >=5.2.0:

Now we’re at the point that we can create the twitter application on the webserver:

  • Unzip both downloaded files and upload them to your webserver (make sure you’re on the right PHP version using the PEAR JSON Class)
  • If not on PHP >= 5.2.0 make sure you include the following piece of code somewhere before getting at the twitter part/including the Oauth library: (sorry for being a bit vage)
if (!function_exists('json_decode')) {
    function json_decode($content, $assoc=false) {
        require_once '/JSON.php';
        if ($assoc) {
            $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        }
        else {
            $json = new Services_JSON;
        }
        return $json-&gt;decode($content);
    }
}

Configurion of OAuth Library

At this point you have to configure the Oauth library to let it communicate with the Twitter API:

  • Go to the path on your FTP server where you’ve uploaded both or separeate classes
  • Edit config.php and make sure you list your customer key, consumer secret and callback url as you listed it at dev.twitter.com
  • Edit twitteroauth.php in the twitteroauth folder, make sure you change the following piece of code:
/**    * Set API URLS    */
function accessTokenURL()  { return 'https://api.twitter.com/oauth/access_token'; }
function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
function authorizeURL()    { return 'https://twitter.com/oauth/authorize'; }
function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }

To:

/**    * Set API URLS    */
function accessTokenURL()  { return 'https://api.twitter.com/oauth/access_token'; }
function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; }
function authorizeURL()    { return 'https://api.twitter.com/oauth/authorize'; }
function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }

Integration with your PHP Code

Now integrate this piece of code into your php page where you want to post tweets to Twitter:

function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
	return $connection;
}
 
// Create a connection with Twitter API using access tokens
$connection = getConnectionWithAccessToken("YOUR_OAUTH_TOKEN", "YOUR_OAUTH_TOKEN_SECRET");
 
// Post message to twitter
$content = $connection->;post('statuses/update', array('status' =>; 'YOUR_TWEET'));

To debug the connection between the Twitter API and your code, use for example print_r($content).

I used the following sites to be able to make the above howto: