libcurl 从curl_easy_perform()获取数据的问题

发布于 2024-10-13 02:14:06 字数 621 浏览 5 评论 0原文

这可能是一个愚蠢的问题,但我如何真正获取curl_easy_perform()返回的站点数据到我可以使用的变量中。当它执行时,我只是在终端上看到它全部闪过。顺便说一句,我正在使用 C。

有什么想法吗?谢谢。

编辑: 这是我正在使用的代码(我正在访问 Twitter Streaming API,我做得正确吗?)

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/filter.json?track=http");
curl_easy_setopt(curl, CURLOPT_USERPWD, "JEggers2:password");
    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);


  }   
  return 0;
}

This is probably a stupid question, but how do I actually get the site data returned by curl_easy_perform() into a variable that I can work with. When it executes, I just see it all flash by on the Terminal. I am using C, by the way.

Any ideas? Thanks.

EDIT:
This is the code I'm using (I'm accessing the Twitter Streaming API, am I even doing it correctly?)

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/filter.json?track=http");
curl_easy_setopt(curl, CURLOPT_USERPWD, "JEggers2:password");
    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);


  }   
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱自由 2024-10-20 02:14:06

要将数据写入字符串,您需要设置一个写入回调函数:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func);

此外,还要设置用于接收数据的字符串变量的地址:

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str)

回调函数将如下所示:

size_t callback_func(void *ptr, size_t size, size_t count, void *stream)
{
      /* ptr - your string variable.
      stream - data chuck you received */

     printf("%.*s", size, (char*)stream);
}

因为您不知道数据的总大小d 正在接收,因此您需要重新分配指针才能将其放入字符串中。

To get the data into string, you need to set up a write callback function:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func);

Also, the address of your string variable to receive the data:

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str)

Callback function would look like this:

size_t callback_func(void *ptr, size_t size, size_t count, void *stream)
{
      /* ptr - your string variable.
      stream - data chuck you received */

     printf("%.*s", size, (char*)stream);
}

Because you won't know the total size of data you'd be receiving so you would need to make pointer re-allocations to get it into a string.

小苏打饼 2024-10-20 02:14:06

另一个答案在 callback_func 的第一个和最后一个参数的使用中似乎是错误的(请参阅文档)。您收到的实际数据块位于第一个参数 ptr 中,而通过 CURLOPT_WRITEDATA 传递的指针是最后一个参数。

我做了一个完整的可编译示例:

#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

size_t dataSize=0;
size_t curlWriteFunction(void* ptr, size_t size/*always==1*/,
                         size_t nmemb, void* userdata)
{
    char** stringToWrite=(char**)userdata;
    const char* input=(const char*)ptr;
    if(nmemb==0) return 0;
    if(!*stringToWrite)
        *stringToWrite=malloc(nmemb+1);
    else
        *stringToWrite=realloc(*stringToWrite, dataSize+nmemb+1);
    memcpy(*stringToWrite+dataSize, input, nmemb);
    dataSize+=nmemb;
    (*stringToWrite)[dataSize]='\0';
    return nmemb;
}

int main()
{
    char* data=0;
    CURL*const curl=curl_easy_init();
    if(!curl)
    {
        fprintf(stderr, "Failed to init curl");
        return 1;
    }
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlWriteFunction);
    if(curl_easy_perform(curl)!=CURLE_OK)
    {
        fprintf(stderr, "Failed to get web page\n");
        return 1;
    }
    curl_easy_cleanup(curl);

    if(!data)
    {
        fprintf(stderr, "Got no data\n");
        return 1;
    }

    printf("Page data:\n\n%s\n", data);
    free(data);
}

The other answer appears to be wrong in its usage of first and last parameters of callback_func (see the docs). Actual chunk of data you received is in the first parameter, ptr, while the pointer you pass with CURLOPT_WRITEDATA is the last parameter.

I've made a complete compilable example:

#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

size_t dataSize=0;
size_t curlWriteFunction(void* ptr, size_t size/*always==1*/,
                         size_t nmemb, void* userdata)
{
    char** stringToWrite=(char**)userdata;
    const char* input=(const char*)ptr;
    if(nmemb==0) return 0;
    if(!*stringToWrite)
        *stringToWrite=malloc(nmemb+1);
    else
        *stringToWrite=realloc(*stringToWrite, dataSize+nmemb+1);
    memcpy(*stringToWrite+dataSize, input, nmemb);
    dataSize+=nmemb;
    (*stringToWrite)[dataSize]='\0';
    return nmemb;
}

int main()
{
    char* data=0;
    CURL*const curl=curl_easy_init();
    if(!curl)
    {
        fprintf(stderr, "Failed to init curl");
        return 1;
    }
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlWriteFunction);
    if(curl_easy_perform(curl)!=CURLE_OK)
    {
        fprintf(stderr, "Failed to get web page\n");
        return 1;
    }
    curl_easy_cleanup(curl);

    if(!data)
    {
        fprintf(stderr, "Got no data\n");
        return 1;
    }

    printf("Page data:\n\n%s\n", data);
    free(data);
}
背叛残局 2024-10-20 02:14:06

函数 writeDataOnStream 有一个名为 buffer 的空指针,该指针保存所有数据(HTML 代码和标头)

#include <iostream>
#include <string.h>
#include <curl/curl.h>

using namespace std;

//ADD THIS FUNCTION: NOTE the Void pointer "buffer"
size_t writeDataOnStream(void * buffer, size_t size, size_t nbytes, void * stream){
    size_t bytes_written = fwrite( buffer, size, nbytes, (FILE *) stream);
    return bytes_written;
}

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
     curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/filter.json?track=http");
     curl_easy_setopt(curl, CURLOPT_USERPWD, "JEggers2:password");

     //APPEND THIS
     curl_easy_setopt(curl, CURLOPT_HEADER, 1L);  //Enable Headers
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeDataOnStream);
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);   //Print data in STDOUT
     //END

     res = curl_easy_perform(curl);
     curl_easy_cleanup(curl);      // always cleanup
  }   
  return 0;
}

CURLOPT_WRITEFUNCTION

CURLOPT_WRITEDATA

The function writeDataOnStream has a void pointer called buffer this pointer hold all data (HTML CODE AND HEADERS)

#include <iostream>
#include <string.h>
#include <curl/curl.h>

using namespace std;

//ADD THIS FUNCTION: NOTE the Void pointer "buffer"
size_t writeDataOnStream(void * buffer, size_t size, size_t nbytes, void * stream){
    size_t bytes_written = fwrite( buffer, size, nbytes, (FILE *) stream);
    return bytes_written;
}

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
     curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/filter.json?track=http");
     curl_easy_setopt(curl, CURLOPT_USERPWD, "JEggers2:password");

     //APPEND THIS
     curl_easy_setopt(curl, CURLOPT_HEADER, 1L);  //Enable Headers
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeDataOnStream);
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);   //Print data in STDOUT
     //END

     res = curl_easy_perform(curl);
     curl_easy_cleanup(curl);      // always cleanup
  }   
  return 0;
}

CURLOPT_WRITEFUNCTION

CURLOPT_WRITEDATA

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文