使用HTTP认证在C libcurl的Twitter的流 [英] Using HTTP authentication with libcurl in C for Twitter Streaming
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/8092870/trying-to-access-twitter-streaming-api-with-c\">Trying用C 访问Twitter流API
块引用>我不知道什么,我在我的code做错了,但它似乎有身份验证的一个问题。我每次执行code没有输出。
在这里我的目标是我想要得到的鸣叫从Twitter API的流。这个问题可能是别的东西。但我不知道。请帮助。
这是C code:
的#include&LT;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;卷曲/ curl.h&GT;结构串{
字符* PTR;
为size_t LEN;
};无效init_string(结构串* S){
S-GT&; LEN = 0;
S-GT&; PTR =的malloc(S-GT&; LEN + 1);
如果(S-GT&; PTR == NULL){
fprintf中(标准错误的malloc()失败\\ n);
出口(EXIT_FAILURE);
}
S-GT&; PTR [0] ='\\ 0';
}为size_t writefunc(无效* PTR,为size_t长度size_t nmemb个,结构串* S)
{
为size_t new_len = S-&GT; LEN +尺寸* nmemb个;
S-GT&; PTR =的realloc(S-GT&; PTR,new_len + 1);
如果(S-GT&; PTR == NULL){
fprintf中(标准错误,realloc()的失败\\ n);
出口(EXIT_FAILURE);
}
的memcpy(S-GT&; PTR + S-&GT; LEN,PTR,大小* nmemb个);
S-GT&; PTR [new_len] ='\\ 0';
S-GT&; LEN = new_len; 返回大小* nmemb个;
}INT主要(无效)
{
卷曲*卷曲;
卷曲code资源; 卷曲= curl_easy_init();
如果(卷曲){
结构字符串s;
init_string(安培; S); curl_easy_setopt(卷曲,CURLOPT_URL,http://stream.twitter.com/1/statuses/sample.json);
curl_easy_setopt(卷曲,CURLOPT_USERPWD,neilmarion:password_here);
curl_easy_setopt(卷曲,CURLOPT_WRITEFUNCTION,writefunc);
curl_easy_setopt(卷曲,CURLOPT_WRITEDATA,&安培; S);
RES = curl_easy_perform(卷曲); 的printf(%S \\ n,s.ptr);
免费(s.ptr); / *总是清理* /
curl_easy_cleanup(卷曲);
}
返回0;
}
解决方案
- 使用
SSL
。您可以使用
curl_easy_strerror()
来得到一个可读的错误消息:的printf(卷曲误差%S,curl_easy_strerror(RES));
- 您会经过一段时间运行内存不足(我到底流〜250KB /秒)。保存有趣的信息到永久存储,并丢弃剩下的。
Possible Duplicate:
Trying to Access Twitter Streaming API with CI am not sure what I am doing wrong in my code but it seems there's a problem on authentication. Every time I execute the code there is no output.
My goal here is I wanted to receive a stream of Tweets from Twitter API. The problem might be something else. But I am not sure. Please help.
This is the C code:
#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: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; }
解决方案
- Use
ssl
.You could use
curl_easy_strerror()
to get a human readable error message:printf("curl error %s", curl_easy_strerror(res));
- You'll run out of memory after a while (at my end the stream is ~250Kb/s). Save interesting info to a persistent storage and discard the rest.
这篇关于使用HTTP认证在C libcurl的Twitter的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!