kvolden
April 4th, 2009, 05:34 PM
Hi, I've been googling for the last two hours without finding the answer to this problem.. I'm using libcurl in a C++ application, and at first I tried without the callback function, which provided the html to the standard output. That worked fine, but when I try to utilize the callback function, as far as I can tell pretty much exactly like the documentation and other confirmed working examples, something goes off the hinges. After a little investigation, I could see that the variable "size_t nmemb" gets a value of "3213310828", and "size_t size" a value of "751"..
The documentation claims that the total size of the received file is size*nmemb, which in my case is obviously wrong, as the real file size is 272413 bytes.
Here's the relevant code:
void Book::update(){
CURL *curl;
CURLcode result;
std::string url;
std::string html = "";
url = "http://www.amazon.co.uk";
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Book::write_html);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);
result = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
size_t Book::write_html(char *buffer, size_t size, size_t nmemb, std::string *html){
int result = 0;
if(buffer != NULL){
html->append(buffer, size*nmemb);
result = size*nmemb;
}
return result;
}
Any help will be highly appreciated! Thanks.
The documentation claims that the total size of the received file is size*nmemb, which in my case is obviously wrong, as the real file size is 272413 bytes.
Here's the relevant code:
void Book::update(){
CURL *curl;
CURLcode result;
std::string url;
std::string html = "";
url = "http://www.amazon.co.uk";
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Book::write_html);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);
result = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
size_t Book::write_html(char *buffer, size_t size, size_t nmemb, std::string *html){
int result = 0;
if(buffer != NULL){
html->append(buffer, size*nmemb);
result = size*nmemb;
}
return result;
}
Any help will be highly appreciated! Thanks.