Babysteps

In this chapter, we’ll take our first babysteps. We are going to talk through the API with Twitter and put the theory from the previous chapter into practice. There’s no programming involved yet, though. We will be using cURL, a command line tool, to do the job.

This is what we will do:

Apart from the installation, the usage of cURL between platform shouldn’t differ. I’m using Microsoft Windows.

Installing cURL

If you haven’t done so, you should download the cURL install package. Select the package matching the operating system you use. Unless you’re eager to compile cURL from source, you want the binary compiled package. Because the Twitter API doesn’t use SSL, you need to download the non-SSL version.

Windows (2000/XP)
Download cURL, unpack and copy curl.exe to c:\windows\System32

Mac OSX
You can download the right binary here. Follow these instructions.

Linux
Choose the binary fitting the linux distribution you use and install it.

Using cURL

cURL is a commandline tool. You will need a shell. In Windows, go to start and select ‘command prompt’ from the ‘all programs’ menu. Alternatively, you can use ‘run…’ and start a shell with the ‘cmd’ command.

All cURL commands in the shell are invoked by the %curl command:

curl_1.jpg

You’ll notice we have to provide a lot of extra arguments and parameters to the cURL command if we want to make good use of it. Let’s try this:

%curl http://dns.be

cURL will fetch the page located at the URL you provide. This can be a static HTML page, but also a parsed PHP/ASP/JSP/… page. In fact, cURL just behaves like a webbrowser. But without parsing and displaying the HTML into a fancy imagerich webpage in a browserscreen.

curl_2.jpg

In this example, the HTML generated by the webserver behind dns.be is fetched by cURL and plainly returned in the shell.

cURL and the Twitter API

Here’s where things get interesting: we are going to use cURL to fetch some Twitter data through it’s API. Suppose we want to get the public timeline using. How do we do that? Well, first of all, you want to use or ‘call’ the right method the API provides. The API documentation refers us to the public_timeline method which is a part of the ’status methods’ group. Using cURL, this is how the call to this method would look:

%curl http://twitter.com/statuses/public_timeline.rss

Notice that I specified the format of the endresult to be RSS. Just give it a shot and try the other output formats. This is the result:

curl_3.jpg

That’s right, our call returned us the last 20 or so tweets that were published on the public timeline in RSS format. You just made your very first call on the Twitter API and got information back without actually surfing to the Twitter website. If you know you your way around some scripting, like bash, you could easily make a script that takes the result from this command, parses it, displays it in a seperate window and refreshes the information every 15 minutes or so.

Twitter Authentication with cURL

The public_timeline methods is a bit of an exception: it doesn’t need authentication. For most of the calls you want to make, you have to authenticate yourself to Twitter with a valid user account before you can take any advantage of them.

Suppose you have an account and you want to get the last 20 tweets you posted on Twitter, you have to authenticate yourself. cURL is build to do just that. It takes an extra argument that let’s you send your credentials with the call you want to make to the Twitter API. If you are authenticated, the call is executed. If your credentials are not validated, the Twitter API will return an error message. This is how the cURL call would look like:

%curl -u username:password http://twitter.com/statuses/user_timeline.rss

And this is the result when we execute the command:

curl_4.jpg

Again, I opted for RSS as the preferred output format.

So far so good. Now that we understand how the Twitter API works and how we can make our own HTTP calls, let’s take it one step further and get started on the actually programming part.