Nov 082012
 
Article PHP

There are several Twitter APIs that developers can use to write applications that interact with the service provided by Twitter.

The first version of the “Search API” allowed to perform anonymous searches in the tweets database without any authentication required. However, in the new 1.1 version of the API all interactions have to be done from a connection athenticated with OAuth (https://dev.twitter.com/docs/api/1.1/overview).

In this post we will explain how to establish a simplified OAuth connection in a single-user application.

Installing the twitteroauth library

First, we download the twitteroauth library from https://github.com/abraham/twitteroauth. The file downloaded is named “twitteroauth-master.tar.gz” and is 21695 bytes in size. The file is uncompressed, and the files in it are extracted with the command:

The uncompressed data is a directory tree including the scripts required to perform a full-blown OAuth implementation. For the purpose of  this post, we just need the OAuth.php and twitteroauth.php files located under the subdirectory “twitteroauth-master/twitteroauth/”.

Script to connect to Twitter

Next, we write the PHP script to connect to twitter:

We can see that the first line of code includes the “”twitteroauth-master/twitteroauth/twiteroaauth.php” library just installed.

But we need to replace the placeholders MY_KEY, MY_CONSUMER_SECRET, MY_TOKEN and MY_OAUTH_SECRET with real values. These values are obtained as shown below:

Obtain the keys and secrets required by the OAuth connection

To do this, we connect to twitter apps at https://dev.twitter.com/apps, and go to “My applications”. If we have not created yet any application, click on the “Create a new application” and go through the screens to create a new application.

Next, clicking on the new application (en this example, “OpenAlfa Tweet Retriever”), the application information page displayed shows the keys and secrets we need:

 

 Posted by at 3:05 pm

 Leave a Reply

(required)

(required)