2

I got the code from C libcurl get output into a string. I modified it and I want to use it to access the Twitter Stream. I added curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/sample.json"); and curl_easy_setopt(curl, CURLOPT_USERPWD, "neilmarion:my_password"); in the code. But the problem is whenever I execute it, there is no output. What must be the problem? Thanks.

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

struct string {
  char *ptr;
  size_t len;
};

void init_string(struct string *s) {
  s->len = 0;
  s->ptr = malloc(s->len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "malloc() failed\n");
    exit(EXIT_FAILURE);
  }
  s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
  size_t new_len = s->len + size*nmemb;
  s->ptr = realloc(s->ptr, new_len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "realloc() failed\n");
    exit(EXIT_FAILURE);
  }
  memcpy(s->ptr+s->len, ptr, size*nmemb);
  s->ptr[new_len] = '\0';
  s->len = new_len;

  return size*nmemb;
}

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

  curl = curl_easy_init();
  if(curl) {
    struct string s;
    init_string(&s);

    curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/sample.json");
    curl_easy_setopt(curl, CURLOPT_USERPWD, "neilmarion:my_password");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
    res = curl_easy_perform(curl);

    printf("%s\n", s.ptr);
    free(s.ptr);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
Community
  • 1
  • 1
neilmarion
  • 2,372
  • 7
  • 21
  • 36
  • What is the value of "res" after the transfer? How about the value of curL_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); – Dan Nov 16 '11 at 04:04

1 Answers1

1

The quickest next step is probably to set CURLOPT_HEADER, to include headers in the body output. Most likely, I would guess it is failing on security, and you'll see the details in the headers.

asc99c
  • 3,815
  • 3
  • 31
  • 54
  • Sir, could you show me how to use this 'curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);' ? I believe that 'writefunc' there must be a callback function. But I am not sure how to code that. Thanks. – neilmarion Nov 11 '11 at 17:09
  • I think you are using it correctly - in fact I think it is better than what I have previously done, as I hadn't seen the WRITEDATA option, and was instead using global variables. Either way, there is an example callback function as I use it here: http://stackoverflow.com/questions/7850716/libcurl-https-post-data-send/7853579#7853579 – asc99c Nov 11 '11 at 20:31