使用HTTP身份validation与C中的libcurl进行Twitter流式传输

可能重复:
尝试使用C访问Twitter Streaming API

我不确定我的代码中出了什么问题,但似乎validation方面存在问题。 每次执行代码时都没有输出。

我的目标是希望从Twitter API接收推文流。 问题可能是其他问题。 但我不确定。 请帮忙。

这是C代码:

#include  #include  #include  #include  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:password_here"); 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; } 

  1. 使用ssl
  2. 您可以使用curl_easy_strerror()来获取人类可读的错误消息:

     printf("curl error %s", curl_easy_strerror(res)); 
  3. 一段时间后你会耗尽内存(在我的末尾,流量约为250Kb / s)。 将有趣的信息保存到持久存储中并丢弃其余存储。