卷曲 - 把输出到变量? [英] cURL - put output into variable?

查看:130
本文介绍了卷曲 - 把输出到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用这个C code:

I'm currently using this C code:

CURL *curl;
CURLcode res;

curl = curl_easy_init();
if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://my-domain.org/");
    res = curl_easy_perform(curl);

    curl_easy_cleanup(curl);
}

它在控制台上打印输出。我怎样才能得到的结果相同,但读入,比如说,一个字符串? (这是一个可能是一个基本的问题,但我还不明白的libcurl API ...)

It prints the output on the console. How can I get the same output, but read it into, say, a string? (This is a probably a basic question, but I do not yet understand the libcurl API...)

感谢您的帮助!

迈克

推荐答案

您需要通过一个函数和缓冲来写缓冲。

You need to pass a function and buffer to write it to buffer.

/* setting a callback function to return the data */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback_func);

/* passing the pointer to the response as the callback parameter */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &response);


/* the function to invoke as the data recieved */
size_t static write_callback_func(void *buffer,
                        size_t size,
                        size_t nmemb,
                        void *userp)
{
    char **response_ptr =  (char**)userp;

    /* assuming the response is a string */
    *response_ptr = strndup(buffer, (size_t)(size *nmemb));

}

请看看更多信息<一个href=\"http://www.dimuthu.org/blog/2009/01/28/making-web-requests-using-curl-from-c-and-php/\">here.

这篇关于卷曲 - 把输出到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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