Last week, I talked about how and why I use Klout as a useful metric. In that post I mentioned that I would be working on a library to use the Klout API with CodeIgniter. Incidentally, my Klout score has dropped a point since then!

When I finally got to writing it up on Friday afternoon I managed to erase all of my work with a poor git decision (still kind of new to git) and had to recode from scratch. In doing so, I pared the library down by using the PHP magic method __call() since the API’s interface is extremely simple and generic.

The API uses either GET or POST, but it is read-only in either case. POST is only used when you are looking for data for more than one user. In my testing, I found that the API will return all the requested information regardless of how you send it over and with how many users, even though the API’s documentation says that multiple user requests need POST.

Using the Klout library is simple, but first you need to register a Mashable account and your application with Klout and add the API key from your account to the config file. All you need to do is load the library and then you can run your code.

Register an account and your application with Klout.

As I said before, the API is pretty simple. There are five endpoints as of Version 8: Klout score, Show user, Topics, Influenced by and Influencer of. All of those endpoints support either JSON or XML as a return format (library default is JSON). You can read the README file and if you need more information on the API itself, go read Klout’s API documentation.

They also have an excellent interactive demo that you can use once you have your API key. The demo allows you to use any of the endpoints with your API in the browser and you are able to pass through whatever users or format you want and it will show you the response headers, body and the target URL that was generated.

Klout IO Docs

Example Code:

This library returns the response from the API directly and doesn’t do any JSON decoding or XML work for any of the standard endpoints. The only exception to this is the convenience profile method that I’ve added, which returns a stdClass object that compiles all the five endpoints for a single user.

You can get the library from the repo or download the latest version directly.

This Klout library is built for use in CodeIgniter (compatible with at least version 1.7 forward), but it can also be used for non-CodeIgniter applications, you’d just need to remove the references to CodeIgniter (the “_ci” instance reference and log_message() usage in the constructor, and the “defined(‘BASEPATH’)” reference at the top) and keep the settings either in a separately loaded config file or at the top of the library’s class declaration. Then, instead of using the CI loader, you would simply ‘include’ the library’s class.