Scripting Twitter with cURL

March 18th, 2007 by Stephane Kattoor

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 password
  • http://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 :


Most Commented Posts

46 Responses to “Scripting Twitter with cURL”

  1. » Scripting Twitter with Perl + LWP « provided by Tech@Sakana Says:

    [...] little follow up on yesterday article about scripting Twitter with cURL: the solution was working, but somehow inconvenient. Not practical to have to go and fetch the user [...]

  2. Terminally Incoherent » Blog Archive » Posting Twitter Updates via Curl Says:

    [...] out it’s this easy. And yes, I can’t take any credit for discovering it – I shamelessly stole the idea from St�phane Kattoor. If you read St�phane’s post, you will see that the relative ease of posting regular [...]

  3. Luke Says:

    Thanks for posting this. I didn’t realize that posting updates was this easy. I hacked up a small shell script so that I can easily update from the command line. :)

  4. Twitter Tools Collection » Freakitude Says:

    [...] Scripting Twitter with Curl [...]

  5. 21 Twitter Tools to Feed Your Twitter Addiction Says:

    [...] Script Twitter with cURL [...]

  6. veky Says:

    Does anybody know how to make it work with Unicode characters? I can’t seem to send them through (the status gets truncated), and usual escaping doesn’t seem to work (ampersand-hash, percent-u, or backslash-u). :-(

  7. Guillaume Riflet Says:

    Hi,
    nice suggestions. It was from this post that I got the idea to make a weather-bot for Lisbon, Portugal.

    http://twitter.com/weatherlisbon

    I describe it in this blog:

    http://webtopmania.blogspot.com/2007/06/twitter-weatherlisbon.html

    Cheers,
    Guillaume

  8. Using Twitter for Software Update Notifications « Macskeeball Says:

    [...] on a combination of Mattias Wikistrom’s Apt Update Script and a Tech@Sakana article called “Scripting Twitter with cURL.” Posted by Macskeeball Filed in [...]

  9. Mighty Linuxz » Scripting Twitter with cURL Says:

    [...] read more | digg story [...]

  10. Ross Goodman Says:

    Hi

    There is now a better API call for sending direct messages. Have a look at this http://www.rossgoodman.com/2008/01/21/my-first-twitterbot/ for my implementation.

    Ross
    http://www.RossGoodman.com

  11. Socialist Sushi » Blog Archive » Savannah weather on Twitter Says:

    [...] original to me, I stole this idea, too. (via this post, which was in one of Jessamyn’s [...]

  12. Christopher Swenson » Blog Archive » curl + twitter + crontab = Happiness Says:

    [...] is easy using the Twitter API through curl. I just wrote the following bash script to update a twitter feed for a specified [...]

  13. Tough Monkey » Blog Archive » 47 Twitter Tools Says:

    [...] Scripting Twitter with Curl [...]

  14. Jeremy Says:

    Is there a way to use the Curl system to change the default “from web” at the end of your tweets. That would be hip.

  15. St�phane Kattoor Says:

    Jeremy,

    I’m afraid not, since this information is added by Twitter according to the source of the tweets …

    Just curious, what would you have wanted to have written instead ?

    Stephane

  16. Smashing Collections » Blog Archive » Twitter Says:

    [...] Scripting Twitter with Curl [...]

  17. fearphage Says:

    The source is just another query param that you pass in.

    source=foo&status=message+here

    Useful example:
    http://my.opera.com/edvakf/blog/2008/07/24/update-twitter-status-from-opera-address-bar

  18. St�phane Kattoor Says:

    ah HA ! Thanks for the info fearphage, that’s cool to know !

    St�phane

  19. dale lane » Blog Archive » Posting to Twitter… carefully Says:

    [...] written back in April as a hackday idea. The code posts Twitter updates using a variation on the twitter-from-curl approach of HTTP POSTing “status=MyTweet” to [...]

  20. RichardBronosky Says:

    Seems that the process of logging in via curl has changed to:
    u=myname;
    p=mypass;
    c=~/.twitter-cookies.txt;
    curl –cookie $c –cookie-jar $c \
    –user-agent Mozilla/4.0 \
    –data “session[username_or_email]=$u” \
    –data “session[password]=$p” \
    –data ‘commit=Sign In’ \
    http://twitter.com/sessions

    This is why Official APIs are important ;-)

  21. Memoria de Acceso Aleatorio » links for 2009-01-20 Says:

    [...] Scripting Twitter with cURL�-�Tech@Sakana – A sysadmin�s blog C�mo usar la herramienta <tt>curl</tt> (Catch URL, obtener URL en ingl�s) para pasar datos a Twitter y realizar diferentes operaciones. Lo estoy usando para mandar actualizaciones a Tweeter sobre el ritmo de la tesis� (tags: programming tutorial bash cURL howto scripting web_services Twitter) [...]

  22. 50 C�ng c? Twitter hay d�nh cho Designers v� Developers | Gi?i Ph�p S? Says:

    [...] Scripting Twitter with cURL cURL is a versatile command-line utility designed to script Web page interactions. As a little demo, this tutorial shows you how to use it to easily to overcome the shortcomings of Twitter�s API. You can also view the Perl + LWP version of this tutorial here: Scripting Twitter with Perl + LWP [...]

  23. http://www.digiwu.com » Blog Archive » 50 Twitter Tools and Tutorials For Designers and Developers Says:

    [...] Scripting Twitter with cURL cURL is a versatile command-line utility designed to script Web page interactions. As a little demo, this tutorial shows you how to use it to easily to overcome the shortcomings of Twitter�s API. You can also view the Perl + LWP version of this tutorial here: Scripting Twitter with Perl + LWP [...]

  24. Mike Says:

    Hmmm…I’m not able to get this guy working, even with RichardBronsky’s suggestions.

  25. Les billets de la semaine #29 - YouBox Says:

    [...] Scripting Twitter via Curl via Sakana.fr [...]

  26. Eric Says:

    Yet another use for my old trusty pal curl.

    I’m gonna give this a try, Thanks.

  27. All Twitter Tools & Tutorials 4 Designers & Developers | Twitter Social Networking Says:

    [...] Scripting Twitter with cURL cURL is a varied command-line programme fashioned to playscript Web tender interactions. As a lowercase demo, this tutorial shows you how to ingest it to easily to overcome the shortcomings of Twitter�s API. You crapper also analyse the Perl + LWP edition of this tutorial here: Scripting Twitter with Perl + LWP [...]

  28. Twitter Search Says:

    thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.

  29. Paul Tow | Freelance tech writer Says:

    [...] based on a combination of Mattias Wikistrom’s Apt Update Script and a Sakana article called “Scripting Twitter with cURL.” [...]

  30. david nahmias Says:

    Hi,
    if you can help

    I am looking for any code that can make a simple search to twitter and get an answer

    Thank You!
    David

  31. ?????? ?? ??????? » ?? ????? ?????? ????? ???????, ????? ??????? [?????? ????]? Says:

    [...] ?????? ??? ??? ???? ???? ??? ????, ??? ????? ?? ????? ??????, ??????? ???? ??? ??? ???? ??????, ?????? ?? ?????? ????? ??????. ?? ???? ?? ??? ????? ????? ???? ????? ???????? ?? ????? ????, ?? ?? ???? ???. (???? ?????? ????) [...]

  32. BillPedersen Says:

    It appears that the …&source=foo option of status update has disappeared, unless someone knows the trick. I see if you do not add source it identifies the course as API but if you add a source=foo you end up with the result of “… from web”.

    Any insight or pointers are greatly appreciated.

    Thanks,

    Bill

  33. To Twitter or Not To Twitter… That is the Question « Techtv101.com Says:

    [...] Scripting Twitter with Curl [...]

  34. Roman Says:

    For all who have problems with some special symbols – try to use unicode and URL encode.
    PHP Functions such as iconv and urlencode with do a good job for you!

  35. Ershad Says:

    Sir, could you please tell me how to append the source parameter (which the twitter uses to fill ‘from’ field ) using curl ?

    Thanks

  36. Bashar Says:

    seems source= always return as from web, any idea how to resolve it ?

  37. shyam Says:

    How can i tweet more than 140 characters with your curl command as explained??

  38. St�phane Kattoor Says:

    Hello Shyam,

    As far as I can tell, you can’t tweet more than 140 characters … that’s the very concept of twitter :-) Of course, you can still split a too long message into 140 characters chunks.

    Regards,
    St�phane

  39. Twitter Response Codes Simplified | NicDev Says:

    [...] Scripting Twitter With cURL – Tech@Sakana [...]

  40. Anthony Says:

    Well thx for this info, I’m gearing up to make first twitter app. However my experience out of the classroom is very limited so.. hell even my classroom is limited.. but I’m going to give this programming a real shot

    Anthony V. Gibby
    Get paid just for using Twitter
    GibbyDevelopments.com
    I’m on Twitter Also

  41. Gideon Says:

    Hi,

    I’m a windows programming and this is pretty interesting? Can I ask how you figure out what commands to send over to twitter?

    The login command on the post doesnt work, curl spits out a login page.

    Also, this –> u=username;p=pass;c=~/.twitter-cookies.txt;curl –cookie $c –cookie-jar $c \–user-agent Mozilla/4.0 \–data “session[username_or_email]=$thegiddygeek” \–data “session[password]=$pass” \–data ‘commit=Sign In’ \http://twitter.com/sessions

    Doesn’t seem to be working either? It says “u is not recognized”

    Thanks
    Gideon

  42. Twitter : Send free SMS messages to your friends - Tech@Sakana – A sysadmin’s blog Says:

    [...] Scripting Twitter with cURL [...]

  43. Geek is a Lift-Style. »Archive » 50 Twitter Tools and Tutorials For Designers and Developers Says:

    [...] Scripting Twitter with cURL cURL is a versatile command-line utility designed to script Web page interactions. As a little demo, this tutorial shows you how to use it to easily to overcome the shortcomings of Twitter’s API. You can also view the Perl + LWP version of this tutorial here: Scripting Twitter with Perl + LWP [...]

  44. [pimp my shell] » Blog Archive » wirklich? so einfach? Says:

    [...] Wichtig: Mit diesem Befehl kann man nur seinen Status veraendern. Moechte man Direktnachrichten ueber twitter versenden ist etwas mehr als nur dieser Einzeiler noetig. [...]

  45. motoculteur Says:

    awesome post, i found all the info i needed right here

  46. Siba Says:

    well this command is working fine if i run through command prompt.But How to execute this command through PHP code.

Leave a Reply