Scripting Twitter with cURL
stephane
Twitter already provides an API but it is currently very limited. It will let you fairly easily change your status, but won’t let you send a direct message.
Comes in cURL. cURL is a very versatile command line utility which is designed to script web pages interactions. As a little demo, I’ll show you how to use it to easily overcome the shortcomings of Twitter’s API.
cURL availability
cURL is available for many Unix/Linux systems as well as for Windows. You can download a binary package from the download page for the cURL project. If you’re using a recent Linux distro, chances are that it is already installed.
Scripting status update
For this very task, we can simply use Twitter’s API :
curl --basic --user username:password --data status="Having fun with cURL" http://twitter.com/statuses/update.xml
Let’s explain this snippet a bit :
--basic --user username:password
: as explained in the API help, the API is password protected by a “Basic Auth”. This takes care of it, by providing your Twitter username and password to the Basic Auth required by the API.--data status="Having fun with cURL"
: this is the data that will be sent to the API, actually the new status, which will be wrapped in a POST HTTP request.http://twitter.com/statuses/update.xml
: last but not least, this is the URL of the API
Scripting a direct message
This requires a little bit more evolved trick. Indeed, Twitter’s API doesn’t provide anything to send a direct message.
To overcome this lack of feature, we are going to use cURL to do this through the web forms, just as if we would do it by hand :
curl --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 --data 'username_or_email=username' --data 'password=password' --data 'commit=Sign In' http://twitter.com/login
This piece performs the login / authentication :
--cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt
: we are reading / storing the cookies from the file /tmp/cookies.txt. Right now this file is empty, but when the authentication will have been done, it will contain the session cookie which will let us keep authenticated at the next request--user-agent Mozilla/4.0
: curl will pretend it is Mozilla/4.0.--data[...]
: the data to be sent, actually the login and passwordhttp://twitter.com/login
: the URL of the login form
This will log us in, and let us send the direct message with the following command :
curl --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 --data "text=little message to send" --data 'commit=Send' http://twitter.com/direct_messages/create/123456
There is not much more to explain with this command. The URL which is used here is the one you can get by going on Twitter, then clicking on the friend you want to send a direct message to, and then clicking on “message” on the right hand sidebar.
That’s it, you’ve sent your Twitter direct message from the command line with cURL ! Of course you could wrap up those two lines in a shell script and make it more user friendly …
Worth reading :
- Twitter : Send free SMS messages to your friends : now you can do this from the command line
- Scripting Twitter with cURL
- Scripting Twitter with Perl + LWP : a script in Perl doing the same thing (much simpler).
- Perl+Twitter : Getting @mentions from command line