尝试使用C访问Twitter Streaming API

我从C libcurl得到了代码输出到字符串 。 我修改了它,我想用它来访问Twitter Stream。 我添加了curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/sample.json");curl_easy_setopt(curl, CURLOPT_USERPWD, "neilmarion:my_password"); 在代码中。 但问题是每当我执行它时,都没有输出。 一定是什么问题? 谢谢。

 #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: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; } 

最快的下一步可能是设置CURLOPT_HEADER,以在正文输出中包含标题。 最有可能的是,我猜它在安全方面失败了,你会看到标题中的细节。