以下答案是使用C ++的C ++方法,而不是以空终止的字符串。它仍然使用回调函数(无法解决),但也使用try / catch处理分配错误。
我想将此curl函数的结果存储在变量中,该怎么办?
#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;
}
您可以设置一个回调函数来使用curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, myfunc);
接收传入的数据块>
回调将采用用户定义的参数,您可以使用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 ++的C ++方法,而不是以空终止的字符串。它仍然使用回调函数(无法解决),但也使用try / catch处理分配错误。
std::string
从此处阅读手册:#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;
}
,我认为您需要多次调用CURL_SETOPT,第一个是要处理的URL,第二个是类似的东西:
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
function_ptr与该签名匹配的地方:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_ptr);
这里发生的是,您表示一个回调函数,当libcurl有一些输出要从调用的任何传输写入时,libcurl就会调用该函数。您可以获取它以自动写入文件,或向其传递指向将自行处理输出的函数的指针。使用此功能,您应该能够将各种输出字符串组装成一个片段,然后在程序中使用它们。
我不确定您可能需要设置哪些其他选项/其他因素会影响您希望应用程序的运行方式,因此请在该页面中仔细查看。
这里是size_t function( void *ptr, size_t size, size_t nmemb, void *stream)
接受的答案的C ++风味>
alex-jasmin
以下答案是使用C ++的C ++方法,而不是以空终止的字符串。它仍然使用回调函数(无法解决),但也使用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;
}
,我认为您需要多次调用CURL_SETOPT,第一个是要处理的URL,第二个是类似的东西:
这里是size_t function( void *ptr, size_t size, size_t nmemb, void *stream)
接受的答案的C ++风味>
alex-jasmin