Example

Example

// Description: Comparator API usage example.
// Copyright: (C) 2015 EffectiveSoft Ltd. All Rights Reserved.
// Technical support: technical-support@effective-soft.com
// This example requires the libcurl and jsoncpp libraries.

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

using namespace std;

// libcurl write callback function
static int writer(char *data, size_t size, size_t nmemb,
				  std::string *writerData)
{
	if (writerData == NULL)
		return 0;
	writerData->append(data, size*nmemb);
	return size * nmemb;
}

int main(int argc, char* argv[])
{
	CURL *curl = NULL;
	CURLcode res;

	// returned data from Intellexer API
	std::string results;
	// libcurl connection initialization
	curl = curl_easy_init();
	if (curl == NULL)
	{
		std::cerr << "Failed to create CURL connection\n";
		return EXIT_FAILURE;
	}
	if(curl) 
	{
		// set the URL for GET request, specify comparison urls and API key for authorization purposes (change YourAPIKey to the Intellexer API key)
		curl_easy_setopt(curl, CURLOPT_URL, "http://api.intellexer.com/compareUrls?apikey=YourAPIKey&url1=http://www.infoplease.com/biography/var/barackobama.html&url2=http://millercenter.org/president/biography/obama-life-in-brief&useCache=false");
		// set option to the callback function
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
		// set option for writing received data
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &results);
		// perform the request
		res = curl_easy_perform(curl);
		// error checking
		if(res != CURLE_OK)
			std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << "\n";
		else
		{
			// parse JSON results
			Json::Value root;
			Json::Reader reader;
			bool isSuccess = reader.parse(results,	root, false);
			if (isSuccess)
			{
				std::cout <<"Proximity between\n";
				const Json::Value url1 = root["document1"];
				const Json::Value url2 = root["document2"];
				const Json::Value proximity = root["proximity"];
				std::cout << url1["title"].asString() << " and " << url2["title"].asString() << " is " << proximity.asString() << "\n"; 
			}
			else
			{
				std::cerr << "JSON parsing error\n";
				return EXIT_FAILURE;
			}
		}
		//cleanup
		curl_easy_cleanup(curl);
	}
	//global cleanup
	curl_global_cleanup();
	return 0;
}

Output

Proximity between
barackobama.html.html and obama-life-in-brief.html is 0.65035585444511101