Using Twitter From Perl

The world and his dog is currently looking at Twitter and eyeing up the possibilities it offers.

I thought i'd jump on the bandwagon, and have a look at the Twitter API.

I wanted to post to a timeline, so the solution is to use one of the update methods. I chose to the XML one, though there is a JSON one also available.

To post to the timeline, Twitter expects an HTTP POST request with a status parameter containing the message you want to post. It associates this to your account by using HTTP's basic authorization functionality.

It's simple to throw together a spot of Perl code to post messages to Twitter knowing this. Have a look at this example...

my $message = "A test post from Perl"; my $req = HTTP::Request->new(POST =>'http://twitter.com/statuses/update.xml'); $req->content_type('application/x-www-form-urlencoded'); $req->content('status=' . $message); $req->authorization_basic($username, $password); my $resp = $ua->request($req); my $content = $resp->content; print $content;

You need to set $username and $password to your username and password, and $message to whatever message you want to appear on your timeline (in this case, "A test post from Perl").

Entered: 2007-03-28 20:58:41

Rob's Other Blog Entries

See other blog entries for March 2007, or an index of all blog entries.