How to write a command line Twitter client in C using libcurl on Debian.
I’ve been playing around with Twitter lately, so naturally I took a look at the Twitter api. Several of the examples are done with curl. For those of you who haven’t looked into the Twitter api, it’s just simple HTTP commands.
To do a status update on Twitter, you do an HTTP post with your username and password, along with the status parameter. This is an easy task using libcurl.
To get started, you need to have gcc and libcurl installed.
apt-get install gcc libcurl3
Here is the example for an HTTP post from the libcurl website.
I used that as my template.
Here is the code:
#include <stdio.h>
#include <curl/curl.h>
#include <string.h> /* Need this header for strcat */
char mesg[140]; /*140 for the tweet */
char status[149]=”status=”;
int main(void)
{
fgets(mesg,140,stdin);
strcat(status,mesg);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, “http://user:pass@twitter.com/statuses/update.json”); /*build the url with the username and password */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, status);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}
That’s all there is to it.
compile with: gcc -lcurl twitter-post.c -o twitter-post
run it:
at@debian:~$ ./twitter-post
this is my status update
Twitter will return a nice blob of JSON information
to let you know it succeeded
Adrian Thiele


•
October 26th, 2009 at 7:23 AM
[...] http://www.debiantoday.com/how-to-write-a-command-line-twitter-client-in-c-using-libcurl-on-debian/... a few seconds ago from seesmic [...]