0

Possible Duplicate:
Trying to Access Twitter Streaming API with C

Could anyone help me how do I code in C of getting a stream of tweets? I already have read how to output results through C libcurl get output into a string. I also know how to get a stream of tweets just through 'curl' in the command terminal. Thanks!

Community
  • 1
  • 1
neilmarion
  • 2,372
  • 7
  • 21
  • 36

2 Answers2

1

If I understand you're asking for the basic pattern of a CURL request, which I personally thought was not that well written in the tutorial. It seemed like a dozen lines of code should provide the basic pattern for how to use CURL, but I didn't see it written like that in a tutorial. Hope this helps:

CURL *handle;
char PostFields[512];

/* initialise curl */
curl_global_init( CURL_GLOBAL_ALL )
handle = curl_easy_init();

/* set options to post */
curl_easy_setopt( handle, CURLOPT_URL, TwitterUrl );
curl_easy_setopt( handle, CURLOPT_POST, 1 );

sprintf( PostFields, "user_id=%s?screen_name=%s", TwitterId, TwitterName );
curl_easy_setopt( handle, CURLOPT_POSTFIELDS, (void*)PostFields );
curl_easy_setopt( handle, CURLOPT_POSTFIELDSSIZE, strlen( PostFields ) );

/* set options to handle response */
curl_easy_setopt( handle, CURLOPT_HEADERFUNCTION, HandleTwitterHeader );
curl_easy_setopt( handle, CURLOPT_WRITEFUNCTION, HandleTwitterResponse );

/* do the request */
curl_easy_perform( handle );
curl_easy_cleanup( handle );
asc99c
  • 3,815
  • 3
  • 31
  • 54
  • Will this output a stream of tweets? Or just a single tweet? – neilmarion Nov 11 '11 at 10:19
  • Neither, it is just example CURL code to post a request - you say you know how to get a stream at the command line, while I am not familiar with the Twitter API. You should be able to use your CURL command line before the ? as the TwitterUrl, and the bit after as your PostFields data. – asc99c Nov 11 '11 at 10:36
  • NB: it sounded like you were originally asking for pointers in writing this code for yourself. If you are looking for it already written, look here: http://code.google.com/p/twitcurl/ – asc99c Nov 11 '11 at 10:37
0

I'm not sure to understand your question, and I am not sure you are understanding it well.

Perhaps your issue is related to JSON in C. There are several C (& C++) libraries for JSON. You could use jansson

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I am sorry for the confusing question. Actually I don't care about outputting the results to a JSON file. What I wanted to actually do is getting a Twitter stream using the curl.h library in C. I edited my question. Thanks. – neilmarion Nov 11 '11 at 08:43
  • According to https://dev.twitter.com/docs/api/1/post/direct_messages/new you should care about JSON. You don't need to put JSON in a file (you could produce JSON into a string and transmit it). – Basile Starynkevitch Nov 11 '11 at 08:52
  • Yes. I know that. Twitter API returns tweets in JSON format. Moreover, I know how to parse a JSON file. What matters most for me is how can I consume the Twitter Stream by coding in C. – neilmarion Nov 11 '11 at 09:08
  • Use curl for the HTTP part, and Jansson for the Json part. – Basile Starynkevitch Nov 11 '11 at 09:11
  • Yes. Exactly. But any sample codes? – neilmarion Nov 11 '11 at 09:14
  • Both curl and Jansson documentation have tutorials with examples. That should be enough. Of course, you need to understand the twitter API. – Basile Starynkevitch Nov 11 '11 at 09:18