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

Articles That May Be Related

Tags: , , , , ,

This entry was posted on Saturday, October 17th, 2009 at 9:51 PM and is filed under Tips and tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “How to write a command line Twitter client in C using libcurl on Debian.”

  1. Vladimir Savi? (firusvg) 's status on Monday, 26-Oct-09 14:23:33 UTC - Identi.ca Says:

    [...] http://www.debiantoday.com/how-to-write-a-command-line-twitter-client-in-c-using-libcurl-on-debian/... a few seconds ago from seesmic [...]

Leave a Reply