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



















[...] 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 [...]
[...] 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 [...]
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.
[...] Scripting Twitter with Curl [...]
[...] Script Twitter with cURL [...]
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).
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
[...] 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 [...]
[...] read more | digg story [...]
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
[...] original to me, I stole this idea, too. (via this post, which was in one of Jessamyn’s [...]
[...] is easy using the Twitter API through curl. I just wrote the following bash script to update a twitter feed for a specified [...]
[...] Scripting Twitter with Curl [...]
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.
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
[...] Scripting Twitter with Curl [...]
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
ah HA ! Thanks for the info fearphage, that’s cool to know !
Stéphane
[...] 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 [...]
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
[...] 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) [...]
[...] 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 [...]
[...] 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 [...]
Hmmm…I’m not able to get this guy working, even with RichardBronsky’s suggestions.
[...] Scripting Twitter via Curl via Sakana.fr [...]
Yet another use for my old trusty pal curl.
I’m gonna give this a try, Thanks.
[...] 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 [...]
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.
[...] based on a combination of Mattias Wikistrom’s Apt Update Script and a Sakana article called “Scripting Twitter with cURL.” [...]
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
[...] ?????? ??? ??? ???? ???? ??? ????, ??? ????? ?? ????? ??????, ??????? ???? ??? ??? ???? ??????, ?????? ?? ?????? ????? ??????. ?? ???? ?? ??? ????? ????? ???? ????? ???????? ?? ????? ????, ?? ?? ???? ???. (???? ?????? ????) [...]
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
[...] Scripting Twitter with Curl [...]
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!
Sir, could you please tell me how to append the source parameter (which the twitter uses to fill ‘from’ field ) using curl ?
Thanks
seems source= always return as from web, any idea how to resolve it ?
How can i tweet more than 140 characters with your curl command as explained??
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
[...] Scripting Twitter With cURL – Tech@Sakana [...]
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
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
[...] Scripting Twitter with cURL [...]
[...] 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 [...]
[...] Wichtig: Mit diesem Befehl kann man nur seinen Status veraendern. Moechte man Direktnachrichten ueber twitter versenden ist etwas mehr als nur dieser Einzeiler noetig. [...]
awesome post, i found all the info i needed right here
well this command is working fine if i run through command prompt.But How to execute this command through PHP code.
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
Do you know if itÅ› stil work?
because if I do
curl –basic –user user:password –data status=”Having fun with cURL” http://twitter.com/statuses/update.xml
I have the answer:
Basic authentication is not supported
well this command is working fine if i run through command prompt
i ll try in php
Hi how can we use follow from text
Profactional, i’ll use it for my blog
you can’t tweet more than 140 characters … that’s the very concept of twitter
[...] 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 [...]
Here is my bash script which will post to Twitter:
http://360percents.com/posts/command…linux-and-mac/
Written in bash using curl. Works perfectly on Ubuntu!
Been looking for this, i will try to put this twitter script on my website, can i use it in php script?