gpt4 book ai didi

c - 使用 libcurl 和 C 访问 Twitter Streaming API

转载 作者:太空宇宙 更新时间:2023-11-03 23:56:37 24 4
gpt4 key购买 nike

我正在尝试用 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 格式的推文。我究竟做错了什么?谢谢

最佳答案

问题是 callback_func 没有返回任何东西。

根据spec ,如果读取成功,您应该返回 size

Function pointer that should match the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *userdata); This function gets called by libcurl as soon as there is data received that needs to be saved. The size of the data pointed to by ptr is size multiplied with nmemb, it will not be zero terminated. Return the number of bytes actually taken care of. If that amount differs from the amount passed to your function, it'll signal an error to the library. This will abort the transfer and return CURLE_WRITE_ERROR.

关于c - 使用 libcurl 和 C 访问 Twitter Streaming API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4752719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com