/* * Libcurl example code by Todd Papaioannou (http://www.luckyspin.org/?p=28) * * Uses the libcurl library (http://curl.haxx.se/) to retrieve a webpage */ #include "Retrievepage.h" using namespace std; using std::string; Retrievepage::Retrievepage() { buffer=""; } Retrievepage::~Retrievepage() { buffer=""; } // This is the writer call back function used by curl int Retrievepage::writer(char *data, size_t size, size_t nmemb,std::string *buffer) { int result = 0; // Is there anything in the buffer? if (buffer != NULL) { // Append the data to the buffer buffer->append(data, size * nmemb); // How much did we write? result = size * nmemb; } return result; } // You know what this does.. void Retrievepage::usage() { cout << "curltest: \n" << endl; cout << " Usage: curltest url\n" << endl; } // Get the page given a url string Retrievepage::getpage(string url) { buffer = ""; // Convert url from string -> char* char* tempurl = (char*)malloc(url.length()+1); strcpy(tempurl, url.c_str()); cout << "% retrieving page for: " << url << endl; // Go get page! string pagesource = run( tempurl ); free( tempurl ); return pagesource; } string Retrievepage::getpagechar(char* url) { buffer = ""; string pagesource = run( url ); return pagesource; } // Actually using libcurl to get page source string Retrievepage::run(char* url) { cout << "Retrieving " << url << endl; // Our curl objects CURL *curl; CURLcode result; curl_global_init (CURL_GLOBAL_ALL); //cout << "delcared objects " << url << endl; //Create our curl handle //cout << "creatng curl handle" << url << endl; curl = curl_easy_init(); //cout << "CREATED curl handle" << url << endl; //cout << "init" << url << endl; if (curl) { // Now set up all of the curl options curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HEADER, 0); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); //cout << "now: retrieving page" << url << endl; // Attempt to retrieve the remote page result = curl_easy_perform(curl); //result = CURLE_OK; //cout << "now: going to cleanup" << url << endl; // Always cleanup curl_easy_cleanup(curl); //cout << "now: just cleanup" << url << endl; // Did we succeed? if (result == CURLE_OK) { cout << "result ok: " << url << endl; //cout << buffer << "\n"; } else { cout << "Error: [" << result << "] - " << errorBuffer; buffer = "Error: " + result; } } else { // Could not create curl handle cout << "Could not create curl handle" << errorBuffer << endl; buffer = "Error: Cannot create curl handle"; } //cout << "leaving function" << url << endl; return buffer; }