cURL-将输出放入变量?
我目前正在使用此C代码:cURL-将输出放入变量?,c,libcurl,C,Libcurl,我目前正在使用此C代码: 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); } 它在控制台上打印输出。如何获得相同的输出,但将其读入(比如)字符串?(这可能是
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);
}
它在控制台上打印输出。如何获得相同的输出,但将其读入(比如)字符串?(这可能是一个基本问题,但我还不了解libcurlapi…)
谢谢你的帮助
Mike您需要传递函数和缓冲区,才能将其写入缓冲区
/* 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));
}
请查看更多信息。您需要一个写回调函数。我使用这种函数读取响应、错误并能够提供自己的标题:
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
std::string buf = std::string(static_cast<char *>(ptr), size * nmemb);
std::stringstream *response = static_cast<std::stringstream *>(stream);
response->write(buf.c_str(), (std::streamsize)buf.size());
return size * nmemb;
}
bool CurlGet(
const std::string &url,
const std::vector<std::string> &headers,
std::stringstream &response,
std::string &error)
{
curl_global_init(CURL_GLOBAL_ALL);
curl_slist *headerlist = NULL;
std::vector<std::string>::const_iterator it;
for (it = headers.begin(); it < headers.end(); it++) {
headerlist = curl_slist_append(headerlist, it->c_str());
}
CURL *curl = curl_easy_init();
char ebuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, ebuf);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(headerlist);
if (res != CURLE_OK)
error = ebuf;
else
error.clear();
return res == CURLE_OK;
}
size\u t write\u数据(void*ptr、size\u t size、size\u t nmemb、void*stream)
{
std::string buf=std::string(静态转换(ptr),大小*nmemb);
std::stringstream*响应=静态广播(流);
响应->写入(buf.c_str(),(std::streamsize)buf.size();
返回大小*nmemb;
}
布尔·柯尔杰(
常量std::字符串和url,
const std::向量和标题,
std::stringstream和response,
std::字符串和错误)
{
curl\u global\u init(curl\u global\u ALL);
curl_slist*headerlist=NULL;
std::vector::const_迭代器it;
for(it=headers.begin();itc_str());
}
CURL*CURL=CURL_easy_init();
char ebuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl,CURLOPT_URL,URL.c_str());
curl_easy_setopt(curl,CURLOPT_NOPROGRESS,1);
curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,ebuf);
curl\u easy\u setopt(curl,CURLOPT\u WRITEFUNCTION,write\u data);
curl_easy_setopt(curl、CURLOPT_WRITEDATA和response);
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headerlist);
CURLcode res=curl\u easy\u perform(curl);
旋度\轻松\清洁(旋度);
卷曲列表(标题列表);
如果(res!=卷曲(OK)
误差=ebuf;
其他的
error.clear();
return res==CURLE\u OK;
}
这可以使用
curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);
它设置一个回调函数write_data
,该函数是一个带有签名的函数
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
如果希望userp
成为程序中使用的某种内部结构,请调用
curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &internal_struct);
要将指向
内部结构的指针传递给write\u data
的每次调用,我解决了回调函数返回代码23到回调函数返回大小的问题
见以下代码:
/* 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));
return ((size_t)(size *nmemb));
//if you not send return value of size it will show you ERROR CODE 23return curl_easy_perform();
}
其他的例子都不适合我。
以下是我最终做的事情:
size_t static curl_write(void *buffer, size_t size, size_t nmemb, void *userp)
{
userp += strlen(userp); // Skipping to first unpopulated char
memcpy(userp, buffer, nmemb); // Populating it.
return nmemb;
}
int GetCurl()
{
CURL *curl;
CURLcode res;
char *s = (char *) malloc(512);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, s);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
printf("GREAT SUCCESS!! Your string is %s\n", s);
}
@Mike,更新了一些示例,以及C和php的链接,基本上是相同的概念。还请注意,您应该在回调函数中返回大小。在这种情况下:返回(size_t)(size*nmemb);即使我想写入文件,也需要回调函数吗?write_callback_func()返回的值是多少?你能详细说明你的答案吗?再添加一点关于你提供的解决方案的描述吗?为什么是“void*userp”?@schaiba-对不起,我不记得了!那是很久以前的事了,我搬走了:)