Posting Twitter Updates via Curl

Here is how to post an update to twitter using just curl and nothing else. I guess you can use this if you are working on some headless unix machine that doesn’t have a proper browser… Of course a machine that has curl would also probably have lynx or elinks but I digress.

Here is how you update twitter with curl:

curl --basic --user username:password --data status="I'm twittering with curl!" http://twitter.com/statuses/update.xml

That’s it! That’s all you have to do. I was actually quite surprised when I found 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 updates, is balanced by totally super retarded mechanics of sending direct messages.

It turns out that curl is not the best way to send them – perl + lwp is.

Based on Stéphane’s notes I wrote a this bash script that lets you send updates. I suppress the xml output and you get the standard curl time counter when you post (let’s say this is a poor man’s progress bar ok?):

#!/bin/bash
read -p "What are you doing? " -e input
curl --basic --user username:password --data status="$input" http://twitter.com/statuses/update.xml > /dev/null
echo "OK"

I’m using the read function, but I could easily convert this to use Xdialog or another graphical input grabbing tool. It would also be worthwhile incorporating Stéphane’s perl script for direct messaging while I’m at it.

Heh… I could actually combine this with my time logging script and have it pop-up and nag me for input every n hours. :P

Update 03/19/2007 08:19:21 PM

Here is the windows version of the script:

@echo off
SET /P input="What are you doing? "
curl --basic --user username:password --data status="%input%" http://twitter.com/statuses/update.xml > NUL
echo OK

You will have to download curl, and drop it somewhere in your %Path%.

[tags]twitter, stéphane kattoor, curl, bash, shell, perl, script[/tags]

This entry was posted in Uncategorized. Bookmark the permalink.



20 Responses to Posting Twitter Updates via Curl

  1. Stéphane UNITED KINGDOM Mozilla Firefox Windows says:

    Hi Luke !

    I’m happy my couple of hacks were useful for someone ;-)

    Cheers

    Stéphane

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Once again thanks for posting this. I love being able to post the updates via curl – and I can easily incorporate this into all kinds of other scripts allowing them to post stuff to twitter. :)

    Reply  |  Quote
  3. Craig Betts UNITED STATES Mozilla Firefox Mac OS Terminalist says:

    A real man would have done this with netcat . . . ;-)

    Nice job Luke!

    Reply  |  Quote
  4. Luke UNITED STATES Mozilla Firefox Windows says:

    I guess it could be done with netcat… I didn’t think about it.

    Reply  |  Quote
  5. Pingback: Twitter Tools Collection » Freakitude UNITED STATES WordPress

  6. Stephane FRANCE Mozilla Firefox Debian GNU/Linux says:

    [quote comment=”3377″]I guess it could be done with netcat… I didn’t think about it.[/quote]

    It can certainly be done with netcat, but that won’t be without pain ! Having to manage the session cookies ? I prefer to leave it to curl :-)

    Reply  |  Quote
  7. Cosmin Ghiu UNITED STATES Mozilla Firefox Mac OS says:

    great blog post. being a programming newbie to twitter was curious to build a small bot that did an auto-reply like post to twitter once a new a new user has followed me. it be something like: @user thank you for following me

    would this type of coding work for this?

    Reply  |  Quote
  8. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Yes, you could use it to send the auto-reply but that’s just half of the problem. You would also need some way of detecting that someone followed you either by querying the twitter API or by using some sort of email filter to trigger it when follow notification comes in.

    So what you are trying to do seems a bit more complex. :)

    Reply  |  Quote
  9. Cosmin Ghiu UNITED STATES Mozilla Firefox Mac OS says:

    thanks for the reply. i knew it wasnt easy since i dont see one available anywhere online. lets see what my coding ninja skills can do. :)

    Reply  |  Quote
  10. I’ve been using this method and just stumbled upon your page while looking for similar methods. One thing I want to point out is that–at least for me and the many other *nix geeks I know who also Twitter from a command line–it has nothing to do with “some headless unix machine that doesn’t have a proper browser…” We use it because it’s QUICKER! I have at least 15 very nice browsers, thank you, but NONE of them is as quick as typing: twitter [my message] (I named the shell script I wrote twitter.) So, yeah! :)

    Reply  |  Quote
  11. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @SmartAssProducts: Agreed. It is indeed faster. :) Also check this post.

    Reply  |  Quote
  12. @Cosmin Ghiu:
    You could build a “follower-thanking” bot as a cron job that:
    1. Capture your latest list of followers like so:

    curl --basic --user $u:$p http://twitter.com/statuses/followers/$u.xml > ~/.twitter-$u-followers.temp

    (The > above should be a > char, but it’s getting caught by the WordPress)
    2. If there is no ~/.twitter-$u-followers.xml, jump to step 7
    3. Compare that to your previously saved* list of followers ~/.twitter-$u-followers.xml
    4. Exit if no diff
    5. Extract the screen_name field for each new follower
    6. Sent each new follower a tweet like so:

    for f in $new; do curl --basic --user $u:$p --data status="@$f Hey, thanks for following me. Where did you find out about me?" http://twitter.com/statuses/update.xml; done

    7. mv -f ~/.twitter-$u-followers.temp ~/.twitter-$u-followers # *this satisfies step 3.

    I’ll write a complete Bash script later, this is just off the top of my head. When I implement Step 5, I’m not sure if I want to just use sed or do a full blown xsltproc on it.

    Reply  |  Quote
  13. Marco Koch SWITZERLAND Safari Mac OS says:

    How can I give a tweet with the source for example “Terminal”.
    Default tweets have the source “web”, “IM”, “phone” or “Twitterific”

    Reply  |  Quote
  14. [quote comment=”11727″]How can I give a tweet with the source for example “Terminal”.
    Default tweets have the source “web”, “IM”, “phone” or “Twitterific”[/quote]

    You would have to get an API key.

    Reply  |  Quote
  15. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    [quote post=”1475″]How can I give a tweet with the source for example “Terminal”. Default tweets have the source “web”, “IM”, “phone” or “Twitterific” [/quote]

    Richard Bronosky is correct. You need to obtain an API key by filling out this form:

    http://twitter.com/help/request_source

    Your application must then be approved by the Twitter team. They mostly decide whether to approve a request or not, based on the project website you submit. Yes, you have to build a custom app, and establish some web presence for it. Creating a Google Code project should be enough, as long as you have some deliverables available for download. That’s what I did.

    More info, here.

    Reply  |  Quote
  16. Pingback: To Twitter or Not To Twitter… That is the Question « Techtv101.com UNITED STATES WordPress

  17. Joe Johnson UNITED STATES Internet Explorer Windows says:

    Thank you. I love tweeting with cURL. It was all I needed to get started with automatic tweets.
    Thanks
    Joemsie

    Reply  |  Quote
  18. Hello folks,

    First good tut for startup,I wonder how secure is this method?

    Reply  |  Quote
  19. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @ Cenk İlker İzanlı:

    You are sending an update via standard http request – it’s not secure at all. If you use https on the other hand, it’s a different story.

    Reply  |  Quote
  20. Dale UNITED STATES Internet Explorer Windows says:

    Can’t get past “Basic authentication is not supported” Has the API changed?

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *