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

标签 c twitter curl streaming libcurl

我正在尝试用 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/

相关文章:

ios - 我可以在 iOS 中自定义 Twitter 工具包的登录按钮吗?

ios - SLRequest - Twitter 返回代码 215(错误的身份验证数据)

java - curl 相当于 -u

更改 C 中 fopen() 的默认搜索目录

c - 高速缓存 Controller 一次从主存到二级高速缓存取多少字节?

C - 写访问冲突

C语言 : implicit pointer dereferencing

android - 无法使用 ShareCompat 在 Twitter 上共享文本

http - 如何在nifi中使用invoke http来执行GET请求?

linux - 如何使用可重定向反编译器 cURL API?