libcurl 输出到变量而不是文本文件

发布于 2024-12-14 00:13:19 字数 2323 浏览 0 评论 0原文

由于未能让 curlpp 用于 C++ 工作,我决定开始将 libcurl 与 C 一起使用(暂时)。对于 C 和 C++ 来说都是全新的,这有点令人困惑。我什至不确定是否可以将 C 和 C++ 函数分开,但据我所知,这是纯 C 的。

在朋友的帮助下,我成功地编写了输出(curl 拾取的页面内容)到文本文件,但我想将其放入字符串变量中,这样我就可以在代码的其他部分使用输出。我可以重新打开文本文件并读取其内容,但这很愚蠢,我想停止写入文件并立即保存到字符串变量。

写入函数

/* the function to invoke as the data received */
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

整个代码

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

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
using namespace std;

/* the function to invoke as the data recieved */
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(int argc, char *argv[]) {

    CURL *curl;
    FILE *fp;
  CURLcode res;

  curl = curl_easy_init();

char outfilename[FILENAME_MAX] = "C:\\Users\\admin\\Downloads\\bbb.txt";

  if(curl) {
char *response = NULL;

        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.*hidden*.org/wp-test/lalala.txt");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);
        fclose(fp);


  }
    return 0;
}

我也希望有人能够详细说明这个函数的确切使用方式。我(从 php 和 vb.net)使用了这样的函数:

function1(ThisIsAvariable,ThisIsAvariableToo)
{
    if ThisIsAvariable = something
    {
         Print "gogopowerrrangers" *append* ThisIsAvariableToo
    }
};

然后会像这样使用:

function1(PassThisAsVariable1,PassThisAsVariable2);

但在上面的代码中,该函数

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)

只是像这样调用

write_data

正如您在这里看到的:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

那么这一切是做什么用的?

(void *ptr, size_t size, size_t nmemb, FILE *stream)

curl 是否会自动“填充”这些内容,或者 C/C++ 的函数工作方式与大多数其他语言不同吗?

In failing to get curlpp for C++ working, I have decided to start using libcurl with C instead (for now). Being completely new to both C and C++, this is getting a little confusing. I'm not even sure if I can keep C and C++ functions apart, but as far as I know, this is pure C.

With the help of a friend, I have managed to write the output (page contents that curl picked up) to a textfile, but I want to put it in a string variable instead, so I can use the output in other parts of the code. I could just re-open the textfile and read its contents but that's silly, I want to stop writing to a file and just save to a string variable immediately.

The write function

/* the function to invoke as the data received */
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

The entire code

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

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
using namespace std;

/* the function to invoke as the data recieved */
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(int argc, char *argv[]) {

    CURL *curl;
    FILE *fp;
  CURLcode res;

  curl = curl_easy_init();

char outfilename[FILENAME_MAX] = "C:\\Users\\admin\\Downloads\\bbb.txt";

  if(curl) {
char *response = NULL;

        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.*hidden*.org/wp-test/lalala.txt");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);
        fclose(fp);


  }
    return 0;
}

I was also hoping someone could elaborate on how this function is used, exactly. I am used (from php and vb.net) to a function like this:

function1(ThisIsAvariable,ThisIsAvariableToo)
{
    if ThisIsAvariable = something
    {
         Print "gogopowerrrangers" *append* ThisIsAvariableToo
    }
};

Which would then be used like:

function1(PassThisAsVariable1,PassThisAsVariable2);

But in the code above, the function

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)

Is simply called like this

write_data

As you can see here:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

So what's all this for?

(void *ptr, size_t size, size_t nmemb, FILE *stream)

Does curl automatically "fill" those in or does C/C++ work differently with functions than most other languages?

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

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

发布评论

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

评论(1

自由如风 2024-12-21 00:13:19

您可以使用字符串流将数据保存在内存中。您可以在 main 中创建字符串流,并将指向它的指针传递给curl,就像您当前将指针传递给FILE 一样。

#include <sstream> // required for std::ostringstream
// ...

std::ostringstream stream;

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);

// call curl like before
// ...

// then get result as a std::string:
std::string output = stream.str();

相应的 write_data 函数如下所示:

size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
    std::ostringstream *stream = (std::ostringstream*)userdata;
    size_t count = size * nmemb;
    stream->write(ptr, count);
    return count;
}

关于函数调用,它的工作方式与其他语言基本相同。如果调用该函数,则必须为其提供参数。本例的不同之处在于,在 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data) 中,不会调用 write_data 函数。 write_data 而是作为 curl_easy_setopt 的参数给出,它通知curl 在需要写入数据时应该使用此函数。

稍后,一旦curl接收到数据并想要写入该数据,它将使用所有适当的参数调用write_data

You could use a string stream to save the data in memory. You would create the string stream in main and pass a pointer to it to curl, like you currently pass a pointer to a FILE.

#include <sstream> // required for std::ostringstream
// ...

std::ostringstream stream;

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);

// call curl like before
// ...

// then get result as a std::string:
std::string output = stream.str();

The according write_data function would look like this:

size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
    std::ostringstream *stream = (std::ostringstream*)userdata;
    size_t count = size * nmemb;
    stream->write(ptr, count);
    return count;
}

And about the function calling, it works basically the same way as in other languages. You have to give parameters to the function if you call it. The difference in this case is, that in curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data) the write_data function isn't called. write_data is instead given as a parameter to curl_easy_setopt which informs curl that is should use this function when it needs to write data.

Later then, once curl receives data and wants to write that data, it will call write_data with all the appropriate parameters.

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