得到一个字符串的输出

我想把这个旋度函数的结果存储在一个变量中,我该怎么做呢?

#include <stdio.h>
#include <curl/curl.h>


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


curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
res = curl_easy_perform(curl);


/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

谢谢,我是这样解决的:

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


function_pt(void *ptr, size_t size, size_t nmemb, void *stream){
printf("%d", atoi(ptr));
}


int main(void)
{
CURL *curl;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
system("pause");
return 0;
}
113835 次浏览

From reading the manual here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html I think you need several calls to CURL_SETOPT, the first being the URL you want to process, the second being something like:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_ptr);

其中 function _ ptr 与此签名匹配:

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

这里要做的是表示一个回调函数,当 libcurl 有一些输出要从您调用的任何传输中写入时,它将调用该函数。您可以让它自动写入文件,或者传递给它一个指向函数的指针,该函数将处理输出本身。使用这个函数,您应该能够将各种输出字符串组装成一个整体,然后在程序中使用它们。

我不知道你还有什么选项可以设置/还有什么会影响你希望你的应用程序如何运行,所以好好看看那个页面吧。

您可以使用 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, myfunc);设置一个回调函数来接收传入的数据块

The callback will take a user defined argument that you can set using curl_easy_setopt(curl, CURLOPT_WRITEDATA, p)

下面是一段代码,它将缓冲区 struct string {*ptr; len}传递给回调函数,并使用 realloc ()在每次调用时增长该缓冲区。

#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, "curl.haxx.se");
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;
}

下面的答案是使用 C + + 的方法,使用 std::string,而不是空终止字符串。它仍然使用回调函数(没有别的办法) ,但也使用 try/catch 处理分配错误。

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


size_t CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, std::string *s)
{
size_t newLength = size*nmemb;
try
{
s->append((char*)contents, newLength);
}
catch(std::bad_alloc &e)
{
//handle memory problem
return 0;
}
return newLength;
}
int main()
{
CURL *curl;
CURLcode res;


curl_global_init(CURL_GLOBAL_DEFAULT);


curl = curl_easy_init();
std::string s;
if(curl)
{


curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");


curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L); //remove this to disable verbose output




/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}


/* always cleanup */
curl_easy_cleanup(curl);
}


std::cout<<s<<std::endl;


std::cout<< "Program finished!" << std::endl;
}

下面是 Alex-Jasmin公认的答案的 C + + 版本

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


size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
{
s->append(static_cast<char *>(ptr), size*nmemb);
return size*nmemb;
}


int main(void)
{
CURL *curl = curl_easy_init();
if (curl)
{
std::string s;


curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);


CURLcode res = curl_easy_perform(curl);


std::cout << s << std::endl;


/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}