使用 libcurl 和 C 访问 Twitter Streaming API
我正在尝试用 C 语言编写一个程序,该程序将从流 API 中获取每条推文并将其存储在数据库中。但是,我无法让它工作,我使用下面的代码:
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
size_t callback_func(void *ptr, size_t size, size_t count, void *stream) {
/* ptr - your string variable.
stream - data chuck you received */
printf("res %s \n \n", (char*)ptr); //prints chunck of data for each call.
}
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com/");
//curl_easy_setopt(curl, CURLOPT_USERPWD, "JEggers2:password");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func);
curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
此代码仅以 JSON 格式输出一条推文。我做错了什么?谢谢
I'm trying to write a program in C which will take each tweet from the streaming API and store it in a database. However, I can't get it to work, I'm using the below code:
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
size_t callback_func(void *ptr, size_t size, size_t count, void *stream) {
/* ptr - your string variable.
stream - data chuck you received */
printf("res %s \n \n", (char*)ptr); //prints chunck of data for each call.
}
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com/");
//curl_easy_setopt(curl, CURLOPT_USERPWD, "JEggers2:password");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func);
curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
This code only outputs one tweet in JSON format. What am I doing wrong? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
callback_func
不返回任何内容。根据 规范,如果读取成功。
The problem is that
callback_func
does not return anything.According to the spec, you should return
size
if the read was successful.