Using LIBCurl with C++ Simple OOP Static Class Example

Posted on July 1st, 2009 by by Amado Martinez

This is a quick class in C++ that I slapped together from an example I found at http://www.luckyspin.org/?p=28.

I’ve been writing a php bot using curl for one of my favorite online games and I decided to make it public. This would be one of my latest public releases in a while. It turned out that PHP is not the best language to use due to poor portability. I played around with bambalam (PHP ‘compiler’) and also WinBinder, but both packages are severely outdated. All my code was written in PHP5, and bambalam only supports php 4. So, Now I’m going back to hardcore programming and coming up with a solution soon. I’ve already got libcurl, and boost hooked up and ready to go!

#include <iostream>
#include "curl/curl.h"

class MyCurl
{
private:

public:
// Write any errors in here

static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
{

// Is there anything in the buffer?
if (buffer_in != NULL)
{
// Append the data to the buffer
buffer_in->append(data, size * nmemb);

// How much did we write?
return size * nmemb;
}

return 0;
}

static std::string get(const char* url)
{
CURL *curl;
CURLcode result;

// Create our curl handle
curl = curl_easy_init();

char errorBuffer[CURL_ERROR_SIZE];
// Write all expected data in here
std::string buffer;

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, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, MyCurl::writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

// Attempt to retrieve the remote page
result = curl_easy_perform(curl);

// Always cleanup
curl_easy_cleanup(curl);
}

if(result == CURLE_OK)
return buffer;
return std::string();
}
};

Tags: ,

Now available for new projects

Posted on June 5th, 2009 by by Amado Martinez

I’m currently available for hire. If you would like to get a quote or a free estimate on your projects, please see the contact or free estimate page and I will get in touch with you then so we can discuss.

New ProjectiveMotion Design

Posted on June 5th, 2009 by by Amado Martinez

I migrated the site from the old static html site to a wordpress installation with a custom theme. The site is now search engine friendly and makes it easier to posts updates. I also think it looks a lot more clean. On the right side of the page, there are links to my getafreelancer and linkedin profile (also new.)

Saltforkoutfitters.org

Posted on June 5th, 2009 by by Amado Martinez

I had been working on a new website saltforkoutfitters.org for a couple of weeks and it’s finally finished. As with all my clients, this site is subject to my 30 day free client support.

Post Categories

Ready Made Templates

Template 27648Template 27639Template 27641Template 27632Template 27611Template 27646Template 27633Template 27634Template 27638

About ProjectiveMotion

We develop cost effective websites for small to mid size businesses. Our passion is customer service and a job well done. We make use of all tools available in order to provide the product to fit your needs.

Do you have a business? Do you have a website? Don't know how to get started? We answer all inquiries within 12 hours. Our products have 30 day free client support.