Unexpected popularity with C++
You know, blogging is such a strange experience. You try to write stuff that will interest people. You try to have a commentary on popular subjects. But you never know what is actually going to be popular. [Of course, I could be writing about the Twitter outages, but why follow the sheep?
] Take my post on using libcurl with C++. This was something that I threw out there a couple of years ago as a result of a small project on which I was working. Just a throwaway post really.
Currently, it accounts for at least half of the new traffic to my blog and a large number of repeat visitors. It’s even the top result in Google when searching for libcurl and C++.

Who knew!!
And I’m not really a C++ fan. Not that it’s not a useful language, but it’s just not really my favorite. Donnie would be amazed!
So the moral of the story? You can’t always choose that for which you are famous. Or, in this case, what generates a bit of traffic to your blog (but certainly doesn’t count as fame!).
Filed under C++ | Comment (0)Using libcurl from C++
For a project I’ve been doing at work I’ve been building an RSS reader in C++. Yeah, I know, me .. C++ .. pigs are flying! Still, that miracle aside, the project has been pretty fun. To get a quick jump start, I began by using the PTypes networking classes to retrieve the RSS feeds. This worked out fine for simple cases but began to get complicated when dealing with server redirects, different HTTP versions, etc. So today I unplugged PTypes and replaced it with libcurl. I should have done this from the start. The easy interface to libcurl makes life.. well. really easy!. And a lot of the cruft I was adding to my code when using PTypes disappeared. This isn’t the fault of PTypes, though. While doing this exercise I wasn’t able to find a simple example of how to wrap libcurl from C++, so I’ve put together one below. I know I could have used cURLpp but I didn’t want to add the extra dependency since I was not really going to use the object bindings it offers to any great extent.
If you find this program useful, please consider a small donation:
/*
* This is a very simple example of how to use libcurl from within
* a C++ program. The basic idea is that you want to retrieve the
* contents of a web page as a string. Obviously, you can replace
* the buffer object with anything you want and adjust elsewhere
* accordingly.
*
* Hope you find it useful..
*
* Todd Papaioannou
*/
#include <string>
#include <iostream>
#include "curl/curl.h"
using namespace std;
// Write any errors in here
static char errorBuffer[CURL_ERROR_SIZE];
// Write all expected data in here
static string buffer;
// This is the writer call back function used by curl
static int writer(char *data, size_t size, size_t nmemb,
std::string *buffer)
{
// What we will return
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 usage()
{
cout < < "curltest: \n" << endl;
cout << " Usage: curltest url\n" << endl;
}
/*
* The old favorite
*/
int main(int argc, char* argv[])
{
if (argc > 1)
{
string url(argv[1]);
cout < < "Retrieving " << url << endl;
// Our curl objects
CURL *curl;
CURLcode result;
// Create our curl handle
curl = curl_easy_init();
if (curl)
{
// Now set up all of the curl options
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, 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);
// Did we succeed?
if (result == CURLE_OK)
{
cout << buffer << "\n";
exit(0);
}
else
{
cout << "Error: [" << result << "] - " << errorBuffer;
exit(-1);
}
}
}
}
Filed under C++, Technology, Updates | Comments (57)



