3

I'm trying to access the Streaming API with C, and am using the following code:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/sample.json?delimited=length -uJEggers2:password");
    res = curl_easy_perform(curl);
    printf("results: %c", res);
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

This prints nothing upon execution, what am I doing wrong? Thanks. EDIT: The Code works when I use google.com as the URL, but it just seems to be an issue with the Streaming API. Any Ideas?

2 Answers2

3

The function curl_easy_perform() only returns the error code. To actually retrieve the data you'll have to set CURLOPT_WRITEFUNCTION to your own callback function.

Edit: You also can't use " -uJEggers2:password" in the URL string, you'll have to set the authentication options using CURLOPT_HTTPAUTH, CURLOPT_USERNAME and CURLOPT_PASSWORD.

Flavio
  • 458
  • 1
  • 3
  • 10
  • That couldn't be right, when I use google.com instead of the Streaming API url the source code is printed. Any ideas? –  Jan 19 '11 at 16:59
  • Curl probably writes the output to stdout when you haven't specified your own WRITEFUNCTION. See http://stackoverflow.com/questions/2329571/c-libcurl-get-output-into-a-string for sample code. – Flavio Jan 19 '11 at 17:00
  • Also see my edit regarding user authentication, this is probably why you didn't see any output. You will still have to use a writefunction if you want to work with the output, otherwise it's just libcurl printing the output to stdout. – Flavio Jan 19 '11 at 17:11
  • Could you please present here how you have solved this. I do not have an idea how should I set CURLOPT_HTTPAUTH to allow Twitter Streaming with C. Thanks! – neilmarion Nov 12 '11 at 02:33
0

edit:I think the difference between using google.com and the twitter API is that in this case you are trying to specify a password with " -u user:pass". Nowhere in the CURL API do I see this technique mentioned.

Instead pass the user and pass separately with an additional curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass") as described in section "NAMES and PASSWORDS OPTIONS" at http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

gravitron
  • 3,514
  • 1
  • 20
  • 18
  • The Code works when I use google.com as the address, it prints the source code. It's just an issue with the Streaming API. Any ideas? –  Jan 19 '11 at 16:56
  • You said in the question that nothing is printed. Do you mean nothing except for "results: __"? – gravitron Jan 19 '11 at 17:03
  • see edit regarding user authentication, same conclusion as Flavio. – gravitron Jan 19 '11 at 17:16
  • 1
    Glad it works. Please consider upvoting and choosing an answer, it'd support people answering your follow-up questions. – gravitron Jan 19 '11 at 20:59