试图用C访问Twitter流API [英] Trying to Access Twitter Streaming API with C

查看:223
本文介绍了试图用C访问Twitter流API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Ç的libcurl的code获取输出到一个字符串。我修改了它,我想用它来访问Twitter的数据流。我加了 curl_easy_setopt(卷曲,CURLOPT_URL,http://stream.twitter.com/1/statuses/sample.json); curl_easy_setopt (卷曲,CURLOPT_USERPWD,neilmarion:MY_PASSWORD); 在code。但问题是,每当我执行它,没有输出。要做就要做到什么问题?谢谢你。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;卷曲/ curl.h>结构串{
  字符* 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-> 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-> 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:MY_PASSWORD);
    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;
}


解决方案

最快的下一步可能是设置CURLOPT_HEADER,包括身体的输出头。最有可能的,我猜它是失败的安全性,你会看到在头部的细节。

I got the code from C libcurl get output into a string. I modified it and I want to use it to access the Twitter Stream. I added curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/sample.json"); and curl_easy_setopt(curl, CURLOPT_USERPWD, "neilmarion:my_password"); in the code. But the problem is whenever I execute it, there is no output. What must be the problem? Thanks.

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

解决方案

The quickest next step is probably to set CURLOPT_HEADER, to include headers in the body output. Most likely, I would guess it is failing on security, and you'll see the details in the headers.

这篇关于试图用C访问Twitter流API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆