I would like to get source code of a page, I have this function to populate a string.
My header file :
class Explorateur {
public:
Explorateur();
~Explorateur();
std::string RecupererCodeSource(std::string pAdresse);
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
};
My class file :
std::string Explorateur::RecupererCodeSource(std::string pAdresse)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, pAdresse);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
std::cout << "curl : " << curl << std::endl;
res = curl_easy_perform(curl);
std::cout << "res : " << res << std::endl;
curl_easy_cleanup(curl);
}
std::cout << "Read : " << readBuffer << std::endl;
return readBuffer;
}
My main :
int main()
{
Explorateur explorateur;
std::string valeur = explorateur.RecupererCodeSource("https://www.google.com/");
return 0;
}
The result is not excepted, it's empty, do you have an idea why ?
curl : 0x1450d60
res : 6
Read :
curl_easy_setopt(curl, CURLOPT_URL, pAdresse);to ` curl_easy_setopt(curl, CURLOPT_URL, pAdresse.c_str());`?curl_easy_perform()reports failure? It is clear that it is having trouble finding Google to begin with, so it can't download anything. And the reason is because you are not passing the correct memory address for the URL string to curl, so it fails to find the server.